Flex DateFormatter以及Date属性
时间:2011-06-16 来源:边缘元素
我们常用下面的三个
public static function GetDate(s:String):Date
{
if (s.length > 0)
{
var sArr:Array = s.split("-");
if (sArr.length >= 3)
return new Date(sArr[0], int(sArr[1])-1, sArr[2]);
}
return new Date(1970, 1, 1);
}
public static function GetDateStr(dt:Date):String
{
var fmt:DateFormatter = new DateFormatter();
fmt.formatString = "YYYY-MM-DD";
return fmt.format(dt);
} public static function format(date:Date,formatString:String):String
{
if(date == null){
return null;
}
var df:DateFormatter = new DateFormatter();
df.formatString = formatString;
return df.format(date);
} 因为Date有构造方法 public function Date(yearOrTimevalue:Object, month:Number, date:Number = 1, hour:Number = 0, minute:Number = 0, second:Number = 0,
millisecond:Number = 0) 因此我们也可以自己写 public static function GetDateTime(s:String):Date //2011-06-11 12:35:13
{
if (s.length > 0)
{
var ss:Array = s.split(" ");
var sArr:Array = ss[0].split("-");
var sArr1:Array = ss[1].split(":");
return new Date(sArr[0], int(sArr[1])-1, sArr[2], sArr1[0], sArr1[1], sArr1[2]);
}
return new Date(1970, 1, 1);
}
public static function GetDateTime2(s:String):Date //2011-06-11
{
if(s == null || s == "")
return new Date(2010,0,1);
else if (s.length > 0)
{
var ss:Array = s.split(" ");
var sArr:Array = ss[0].split("-");
return new Date(sArr[0], int(sArr[1])-1, sArr[2]);
}
return new Date(2010,0,1);
} public static function GetMinutes(s:String):int
{
var dt:Date = GetDateTime(s);
var dtNow:Date = new Date();
var diff:Number = (dtNow.getTime() - dt.getTime()) /1000;
return diff/60;
}
public static function GetWaitingDate(s:String):String
{ var dt:Date = GetDateTime(s);
var dtNow:Date = new Date();
var diff:Number = (dtNow.getTime() - dt.getTime()) /1000; //Alert.show(GetDateStr2(dt));
var sRet:String = "";
var iHour:int = 0;
var iMinute:int = 0;
var iSec:int = 0;
if (diff > 3600*24)
{
var iDay:int = diff / (3600*24);
//sRet = iDay + "天";
diff = diff % (3600*24);
}
iHour = diff/3600;
if (iHour > 0)
diff = diff % 3600;
iMinute = diff/60;
if (iMinute > 0)
diff = diff % 60;
iSec = diff;
var sHour:String = iHour.toString();
if (sHour.length == 1)
sHour = "0" + sHour;
var sMin:String = iMinute.toString();
if (sMin.length == 1)
sMin = "0" + sMin;
var sSec:String = iSec.toString();
if (sSec.length == 1)
sSec = "0" + sSec;
sRet = sRet + sHour + ":" + sMin + ":" + sSec;
return sRet;
} Date 容易混淆的属性date和day date : Number 按照本地时间返回 Date 对象所指定的表示月中某天的值(1 到 31 之间的一个整数)。
{
if (s.length > 0)
{
var sArr:Array = s.split("-");
if (sArr.length >= 3)
return new Date(sArr[0], int(sArr[1])-1, sArr[2]);
}
return new Date(1970, 1, 1);
}
public static function GetDateStr(dt:Date):String
{
var fmt:DateFormatter = new DateFormatter();
fmt.formatString = "YYYY-MM-DD";
return fmt.format(dt);
} public static function format(date:Date,formatString:String):String
{
if(date == null){
return null;
}
var df:DateFormatter = new DateFormatter();
df.formatString = formatString;
return df.format(date);
} 因为Date有构造方法 public function Date(yearOrTimevalue:Object, month:Number, date:Number = 1, hour:Number = 0, minute:Number = 0, second:Number = 0,
millisecond:Number = 0) 因此我们也可以自己写 public static function GetDateTime(s:String):Date //2011-06-11 12:35:13
{
if (s.length > 0)
{
var ss:Array = s.split(" ");
var sArr:Array = ss[0].split("-");
var sArr1:Array = ss[1].split(":");
return new Date(sArr[0], int(sArr[1])-1, sArr[2], sArr1[0], sArr1[1], sArr1[2]);
}
return new Date(1970, 1, 1);
}
public static function GetDateTime2(s:String):Date //2011-06-11
{
if(s == null || s == "")
return new Date(2010,0,1);
else if (s.length > 0)
{
var ss:Array = s.split(" ");
var sArr:Array = ss[0].split("-");
return new Date(sArr[0], int(sArr[1])-1, sArr[2]);
}
return new Date(2010,0,1);
} public static function GetMinutes(s:String):int
{
var dt:Date = GetDateTime(s);
var dtNow:Date = new Date();
var diff:Number = (dtNow.getTime() - dt.getTime()) /1000;
return diff/60;
}
public static function GetWaitingDate(s:String):String
{ var dt:Date = GetDateTime(s);
var dtNow:Date = new Date();
var diff:Number = (dtNow.getTime() - dt.getTime()) /1000; //Alert.show(GetDateStr2(dt));
var sRet:String = "";
var iHour:int = 0;
var iMinute:int = 0;
var iSec:int = 0;
if (diff > 3600*24)
{
var iDay:int = diff / (3600*24);
//sRet = iDay + "天";
diff = diff % (3600*24);
}
iHour = diff/3600;
if (iHour > 0)
diff = diff % 3600;
iMinute = diff/60;
if (iMinute > 0)
diff = diff % 60;
iSec = diff;
var sHour:String = iHour.toString();
if (sHour.length == 1)
sHour = "0" + sHour;
var sMin:String = iMinute.toString();
if (sMin.length == 1)
sMin = "0" + sMin;
var sSec:String = iSec.toString();
if (sSec.length == 1)
sSec = "0" + sSec;
sRet = sRet + sHour + ":" + sMin + ":" + sSec;
return sRet;
} Date 容易混淆的属性date和day date : Number 按照本地时间返回 Date 对象所指定的表示月中某天的值(1 到 31 之间的一个整数)。
getDate():Number按照本地时间返回 Date 对象指定的月中某天的值(1 到 31 之间的一个整数)。
day : Number
[只读 (read-only)] 按照本地时间返回该 Date 所指定的星期值(0 代表星期日,1 代表星期一,依此类推)。
getDay():Number按照本地时间返回该 Date 所指定的星期值(0 代表星期日,1 代表星期一,依此类推)。
相关阅读 更多 +