// ---------------------------------------
// The script in fla's frame:
//----------------------------------------
import plugs.data.DateHelper;
var tf:TextField = new TextField();
tf.multiline = tf.wordWrap = tf.background = true;
tf.autoSize = "left";
tf.width = 250;
addChild(tf);
var curDate:Date = new Date();
tf.appendText("\n" + curDate.toString());
var lastDate:Date = DateHelper.subtractDate(curDate, 0, 27, 3, 4);
tf.appendText("\n" + lastDate.toString());
var cur:Date = DateHelper.addDate(lastDate, 1, 3, 3, 4);
tf.appendText("\n" + cur.toString());
var difIna:String = DateHelper.difIndonesian(lastDate, curDate);
tf.appendText("\n" + difIna);
tf.appendText("\n" + DateHelper.difEnglish(lastDate, curDate));
-------------------------------------------------------
The result:
-------------------------------------------------------
Sun Feb 28 23:38:10 GMT+0700 2010
Sat Feb 27 20:35:06 GMT+0700 2010
Sun Feb 28 23:38:10 GMT+0700 2010
1 hari 3 jam 3 menit 4 detik
1 days 3 hours 3 minutes 4 seconds
-------------------------------------------------------
The helper class:
-------------------------------------------------------
/**
* Flash 10
* plugs framework
* DateHelper
* mi_kuncoro@yahoo.co.id
* www.icesflash.com
*//
package plugs.data
{
public class DateHelper
{
static protected const msS:Number = 1000;
static protected const msM:Number = 60000;
static protected const msH:Number = 3600000;
static protected const msD:Number = 86400000;
static public function addDate(dt:Date, d:Number, h:Number=0, m:Number=0, s:Number=0):Date
{
var v:Number = dt.valueOf() + d * msD + h * msH + m * msM + s * msS;
return new Date(v);
}
static public function subtractDate(dt:Date, d:Number, h:Number=0, m:Number=0, s:Number=0):Date
{
var v:Number = dt.valueOf() - d * msD - h * msH - m * msM - s * msS;
return new Date(v);
}
static public function difDate(dA:Date, dB:Date):Object
{
var n:Number = Math.abs(dA.valueOf() - dB.valueOf());
var ret:Object = new Object();
ret.day = Math.floor(n/msD);
var n0:Number = n % msD;
ret.hour = Math.floor(n0/msH);
var n1:Number = n0 % msH;
ret.minute = Math.floor(n1/msM);
var n2:Number = n1 % msM;
ret.second = n2 / msS;
return ret;
}
static public function difEnglish(dA:Date, dB:Date):String
{
var o:Object = difDate(dA, dB);
return o.day + " days "
+ o.hour.toString() + " hours "
+ o.minute.toString() + " minutes "
+ o.second.toString() + " seconds";
}
static public function difIndonesian(dA:Date, dB:Date):String
{
var o:Object = difDate(dA, dB);
return o.day + " hari "
+ o.hour.toString() + " jam "
+ o.minute.toString() + " menit "
+ o.second.toString() + " detik";
}
static public function difDay(dA:Date, dB:Date):Number
{
return (dA.valueOf() - dB.valueOf())/msD;
}
static public function difHour(dA:Date, dB:Date):Number
{
return (dA.valueOf() - dB.valueOf())/msH;
}
static public function difMinute(dA:Date, dB:Date):Number
{
return (dA.valueOf() - dB.valueOf())/msM;
}
static public function difSecond(dA:Date, dB:Date):Number
{
return (dA.valueOf() - dB.valueOf())/msS;
}
}
}
Langganan:
Posting Komentar (Atom)
awesome code! thanks it helped me alot!
BalasHapusgreat code! thx, it's very usefull to me.
BalasHapusGreg