Skip to content

Instantly share code, notes, and snippets.

@liekkas
Created May 16, 2012 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liekkas/2711552 to your computer and use it in GitHub Desktop.
Save liekkas/2711552 to your computer and use it in GitHub Desktop.
ActionScript3:日期工具类
package com.ultrapower.tomas.special.utils
{
import mx.formatters.DateFormatter;
/*************************************************************\
* 相关日期工具类
*
* @author liekkas.zeng
* @since 2012.03.31
\*************************************************************/
public class DateUtils
{
private static var df:DateFormatter = new DateFormatter();
/**
* 根据日期字符返回日期类型
* 例如:2012-03-31 18:37:46
* */
public static function string2Date(parStr:String):Date
{
var arr1:Array = parStr.split(' ');
if(arr1.length==2)
{
var dateAry:Array = (arr1[0] as String).split('-');
var timeAry:Array = (arr1[1] as String).split(':');
return new Date(dateAry[0],dateAry[1],dateAry[2],timeAry[0],timeAry[1],timeAry[2]);
}
return new Date();
}
/**
* 日期转字符串
* */
public static function date2String(date:Date,format:String="YYYY-MM-DD HH:NN:SS"):String
{
df.formatString = format;
return df.format(date);
}
/** 年 月 日 计算
* datepart值:"fullyear":"month":"date":"hours":"minutes":"seconds":"milliseconds":
* ("fullYear", 2)两年后
* ("fullYear", -2)两年前
* ("month", 11)11个月后的日期
* ("month", 11)11个月前的日期
* ("date", 4)4天后的日期
* ("date", -4)4天前的日期
* */
public static function dateAdd(datepart:String = "", number:Number = 0, date:Date = null):Date
{
if (date == null) {
date = new Date();
}
var returnDate:Date = new Date(date.time);;
switch (datepart.toLowerCase()) {
case "fullyear":
case "month":
case "date":
case "hours":
case "minutes":
case "seconds":
case "milliseconds":
returnDate[datepart] += number;
break;
default:
break;
}
return returnDate;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment