flex常用的日期处理函数
?
?
?
/*获取本年度之前10年的LIST*/
public function yearComBox():void{
? ? var yearList:ArrayCollection = new ArrayCollection();
? ? var dt:Date=new Date();
? ? var curYear:Number=dt.fullYear;
? ? var firstItem:Object=new Object();
? ? firstItem.name=curYear as String;
? ? firstItem.value=curYear as String;
? ? for (var i:int=0; i < 11; i++){
var item:Object=new Object();
var tempYear:Number=curYear - i;
var tempYearStr:String=tempYear.toString();
item["name"]=tempYearStr;
item["value"]=tempYearStr;
yearList.addItem(item);
? ? }
}
?
/*获得两个日期之差 */
public static function getDateDiff(startDate:Date, endDate:Date):int?
{
var diff:Number = (Number(endDate) - Number(startDate))/(3600000*24);
return diff;
}
/* 获得现在日期 */
public static function getDate():Number
{
return new Date();
}
/* 获得现在日期的月份 */
public static function getMonth():Number
{
return new Date().getMonth() + 1;
}
public static function getFullYear():Number
{
return new Date().getFullYear();
}
/* 获得给定年的天数 */
public static function getDaysOfYear(year:int):int?
{
var startDate:Date = new Date(year, 0, 1);
var endDate:Date = new Date(year+1, 0, 1);
return (getDateDiff(startDate, endDate));
}
/* 获得给定年、月的天数 */
public static function getDaysOfMonth(year:int, month:int):int
{
var startDate:Date = new Date(year, month, 1);
var endDate:Date = new Date(year, month + 1, 1);
return (getDateDiff(startDate, endDate));
?
}
//获得给定年 全年月份的天数
public static function getMonthlyDayCounts(year:int):Array
{
var result:Array = [];
var startDate:Date;
var endDate:Date;
var monthlyDayCount:int;
for (var i:int = 0; i < 12; i++)?
{
startDate = new Date(year, i, 1);
endDate = new Date(year, i +1, 1);
monthlyDayCount = getDateDiff(startDate, endDate);?
result.push(monthlyDayCount);
}
return result;
}
/* 比较两个日期大小 */
public static function compare(date1:Date, date2:Date):int?
{
if (Number(date1) < Number(date2))?
{
return -1
}
if (Number(date1) == Number(date2))?
{
return 0;
}
return 1;
}
//把date日期转化为String
public static function convertToDateString(date:Date):String
{
var year:String = date.getFullYear().toString();
var month:String = Number(date.getMonth() + 1) < 10 ? "0" + String(date.getMonth() + 1): String(date.getMonth() + 1);
var dates:String = Number(date.getDate()) < 10 ? "0" + String(date.getDate()): String(date.getDate());
return year +"-"+ month+"-"+ dates;
}