Flash 有好的AS3日期库吗?
我很好奇,是否有人能给我指出一个健壮的、有良好文档记录的AS3日期库。具体地说,我想要一个像Java date formatter那样工作的(至少在原则上是这样),这样您就可以传入一个特定类型的字符串并返回一个日期对象,然后可以轻松地转换为另一个时区,等等。当您需要做这样的事情时,使用本机日期对象是一个令人难以置信的PITA。。。我希望我不必从头开始重新发明这个轮子Flash 有好的AS3日期库吗?,flash,actionscript-3,date,Flash,Actionscript 3,Date,我很好奇,是否有人能给我指出一个健壮的、有良好文档记录的AS3日期库。具体地说,我想要一个像Java date formatter那样工作的(至少在原则上是这样),这样您就可以传入一个特定类型的字符串并返回一个日期对象,然后可以轻松地转换为另一个时区,等等。当您需要做这样的事情时,使用本机日期对象是一个令人难以置信的PITA。。。我希望我不必从头开始重新发明这个轮子 TIA您可能会从CASA Lib这样的框架中找到一个有用的工具。这里有一个例子: 还有其他一些大的库可能有一些有用的东西,比如AS
TIA您可能会从CASA Lib这样的框架中找到一个有用的工具。这里有一个例子:
还有其他一些大的库可能有一些有用的东西,比如AS3CoreLib:好的,嗯。。。看到没有其他人插话,我继续写我自己的。它记录了它作为构造函数的期望,以及如何使它格式化日期并将其转换为备用时区 所有关于它的常规内容都无法保证它的工作效果(或者根本无法保证) 它没有任何类型的外部依赖关系。只需创建一个具有匹配名称的.as文件(如果您愿意,可以随意重新命名),然后就可以进城了 所有的公共功能都位于顶部,只有少量文档(并非完全不言自明) 希望对你有帮助
package {
public class DrDredelDate {
/*
init new date by passing:
a) nothing - an object with the current time will be generated
b) a millisecond string for a specific time
c) a time formatted in one of the following two ways:
2013-01-01 00:00:00 -7:00 -- timzeone is optional, if omitted, date will be created as GMT
or
2012-01-18T23:30:42-08:00 -- timezone non-optional
*/
public function DrDredelDate(inD:String = null) {
if(inD == null ){
d = new Date();
getDateVars(d)
}
else if(inD.replace(/\d+/,"") == ""){
d = new Date(parseInt(inD))
getDateVars(d);
}
else
d = convertFromString(inD);
}
/**
Format using the following control characters:
m = month,
d = day,
yy = 2 digit year,
yyyy = 4 digit year,
h = 24 hour time,
M = minute,
s = second,
t = timezone as digits offset,
c = month abbr,
W = weekday
H = 12hour
a = am/pm
A = AM/PM
T = timezone abbr //if available
*/
public function getFormattedDate(formatString:String):String{
var ret:String = "";
for(var i=0;i<formatString.length; i++){
if(formatString.charAt(i) == 'm'){
if(formatString.substring(i).match(/^mm/)){
ret += addLeadingZero(month);i++;continue;
}ret += month;continue;
}
if(formatString.charAt(i) == 'd'){
if(formatString.substring(i).match(/^dd/)){
ret += addLeadingZero(date);i++;continue;
}ret += date;continue;
}
if(formatString.charAt(i) == 'y'){
if(formatString.substring(i).match(/^yyyy/)){
ret += year;i+=3;continue;
}else if(formatString.substring(i).match(/^yy/)){
ret += (year+"").substring(2,4);i++;continue;
}
}
if(formatString.charAt(i) == 'h'){
if(formatString.substring(i).match(/^hh/)){
ret += addLeadingZero(hours);i++;continue;
}ret += hours;continue;
}
if(formatString.charAt(i) == 'H'){
if(formatString.substring(i).match(/^HH/)){
ret += addLeadingZero(militaryToClockHour());i++;continue;
}ret += militaryToClockHour();continue;
}
if(formatString.charAt(i) == 'a'){
ret += ampm();continue;
}
if(formatString.charAt(i) == 'A'){
ret += AMPM();continue;
}
if(formatString.charAt(i) == 'M'){
if(formatString.substring(i).match(/^MM/)){
ret += addLeadingZero(minutes);i++;continue;
}ret += minutes;continue;
}
if(formatString.charAt(i) == 's'){
if(formatString.substring(i).match(/^ss/)){
ret += addLeadingZero(seconds);i++;continue;
}ret += seconds;continue;
}
if(formatString.charAt(i) == 't'){
ret += timezoneOffset;continue;
}
if(formatString.charAt(i) == 'T'){
ret += timezoneString;continue;
}
if(formatString.charAt(i) == 'c'){
ret += getMonthAbbr();continue;
}
if(formatString.charAt(i) == 'W'){
ret += getWeekday();continue;
}
ret += formatString.charAt(i);
}
return ret;
}
/**
Usage:
Returns a formatted string representation of this Date object converted over into the requested timezone. Note that this has no effect on the underlying date object and the returned value is simply a string representation.
arguments :
format: pass in the same format as you would for the method above for the format param
timeZoneOffset: pass in an abbr or one of the US tZones from mySQL or just a numeric offset representing the hour offset from GMT ie. -5 or +3
*/
public function getShiftedTimezoneFormattedDate(format:String, timeZoneOffset:String = "GMT"):String{
var tzOffset = null;
if( timeZoneOffset.toUpperCase().replace(/[^A-Z]/g,"") == "GMT")
tzOffset = 0;
if( timeZoneOffset.toUpperCase().replace(/[^A-Z]/g,"") == "EST" || timeZoneOffset == "US/Eastern")
tzOffset = -5;
if(timeZoneOffset.toUpperCase().replace(/[^A-Z]/g,"") == "EDT")
tzOffset = -4;
if(timeZoneOffset.toUpperCase().replace(/[^A-Z]/g,"") == "CST" || timeZoneOffset == "US/Central")
tzOffset = -6;
if(timeZoneOffset.toUpperCase().replace(/[^A-Z]/g,"") == "CDT")
tzOffset = -5;
if(timeZoneOffset.toUpperCase().replace(/[^A-Z]/g,"") == "MST" || timeZoneOffset == "US/Mountain")
tzOffset = -7;
if(timeZoneOffset.toUpperCase().replace(/[^A-Z]/g,"") == "MDT")
tzOffset = -6;
if(timeZoneOffset.toUpperCase().replace(/[^A-Z]/g,"") == "PST" || timeZoneOffset == "US/Pacific")
tzOffset = -8;
if(timeZoneOffset.toUpperCase().replace(/[^A-Z]/g,"") == "PDT")
tzOffset = -7;
if(tzOffset == null){
var operator = timeZoneOffset.charAt(0);
tzOffset = parseInt(timeZoneOffset.replace("/:.*/",""));
}
if(isNaN(tzOffset))
return null
tzOffset = tzOffset * 60 * 60 * 1000;
var dTzOffset = -d.timezoneOffset * 60 * 1000;
var newMS = d.getTime() - dTzOffset + tzOffset;
var nDate:Date = new Date(newMS);
return new DrDredelDate(nDate.getTime() + "").getFormattedDate(format);
}
public function isAfterDate(comparisonDate:Date):Boolean{
return(d.time > comparisonDate.time)
}
public function isSameAsDate(comparisonDate:Date):Boolean{
return(d.time == comparisonDate.time)
}
public function getTime(){
return d.time;
}
public function getDate(){
return d;
}
///////////NOTHING TO REALLY LOOK AT BELOW HERE////////////////
public static var PST:String = "PST";
public static var EST:String = "EST";
public static var CST:String = "CST";
public static var MST:String = "MST";
private var d:Date;
public var year,month,monthName,date,weekDay,hours,minutes,seconds,timezoneOffset;
private function getDateVars(d:Date){
this.year = d.fullYear;
this.month = d.month;
this.date = d.date;
this.hours = d.hours;
this.minutes = d.minutes;
this.seconds = d.seconds;
this.timezoneOffset = d.timezoneOffset;
}
private function convertFromString(inD:String):Date{
//2012-10-03 10:15:00 GMT-0800
if(inD.match(/\d{4}\-\d{1,2}\-\d{1,2} \d{1,2}\:\d{1,2}\:\d{1,2}/)){
return timeFromMySqlTimestamp(inD);
}
//2012-01-18T23:30:00-08:00
if(inD.match(/\d{4}\-\d{1,2}\-\d{1,2}T\d{1,2}\:\d{1,2}\:\d{1,2}/)){
return timeFromGenericTimestamp(inD);
}
return null;
}
private var monthAbbr = ['','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
public function getWeekday(){
return d.toString().split(" ")[0];
}
public function getMonthAbbr(){
return d.toString().split(" ")[1];
}
private function timeFromMySqlTimestamp(inD:String):Date{
//2012-10-19 03:15:22 PST
var parts:Array = inD.split(" ");// 0 = date, 1 = hours, 2 = timezone
year = stripLeadingZero(parts[0].split("-")[0]);
month = stripLeadingZero(parts[0].split("-")[1]);
date = stripLeadingZero(parts[0].split("-")[2]);
hours = stripLeadingZero(parts[1].split(":")[0]);
minutes = stripLeadingZero(parts[1].split(":")[1]);
seconds = stripLeadingZero(parts[1].split(":")[2]);
try{
timezoneOffset = getTZFromString(parts[2]);
}catch(e:Error){
timezoneOffset = "GMT-000";
}
return new Date(monthAbbr[month] + " " + date + " " + year + " " + hours + ":" + minutes + ":" + seconds + " " + timezoneOffset);//09 03 2012 23:39:05 GMT-0500
}
private var timezoneString = "";
private function getTZFromString(inStr:String){
if(inStr.toUpperCase().replace(/[^A-Z]/g) == "EST"){timezoneString = "EST"; return "GMT-0500"};
if(inStr.toUpperCase().replace(/[^A-Z]/g) == "EDT"){timezoneString = "EDT"; return "GMT-0400"};
if(inStr.toUpperCase().replace(/[^A-Z]/g) == "CST"){timezoneString = "CST"; return "GMT-0600"};
if(inStr.toUpperCase().replace(/[^A-Z]/g) == "CDT"){timezoneString = "CDT"; return "GMT-0500"};
if(inStr.toUpperCase().replace(/[^A-Z]/g) == "MST"){timezoneString = "MST"; return "GMT-0700"};
if(inStr.toUpperCase().replace(/[^A-Z]/g) == "MDT"){timezoneString = "MDT"; return "GMT-0600"};
if(inStr.toUpperCase().replace(/[^A-Z]/g) == "PST"){timezoneString = "PST"; return "GMT-0800"};
if(inStr.toUpperCase().replace(/[^A-Z]/g) == "PDT"){timezoneString = "PDT"; return "GMT-0700"};
if(inStr.charAt(0) == "-" || inStr.charAt(0) == "+") return "GMT" + inStr;
return inStr;
}
private function timeFromGenericTimestamp(inD:String):Date{
//2012-01-18T23:30:00-08:00
var parts:Array = inD.split("T");// 0 = date, 1 = hours, 2 = timezone
year = stripLeadingZero(parts[0].split("-")[0]);
month = stripLeadingZero(parts[0].split("-")[1]);
date = stripLeadingZero(parts[0].split("-")[2]);
hours = stripLeadingZero(parts[1].split(":")[0]);
minutes = stripLeadingZero(parts[1].split(":")[1]);
var secAndTZ = parts[1].split(":")[2];
var plusMinusTok = (secAndTZ.indexOf("-") != -1)? "-" : "+";
seconds = stripLeadingZero(secAndTZ.split(plusMinusTok)[0]);
timezoneOffset = plusMinusTok + secAndTZ.split(plusMinusTok)[1] + "00";
return new Date(monthAbbr[month] + " " + date + " " + year + " " + hours + ":" + minutes + ":" + seconds + " " + timezoneOffset);//09 03 2012 23:39:05 GMT-0500
}
private function addLeadingZero(inD:Number):String{
return ( inD >= 10) ? inD + "" : "0" + inD;
}
private function stripLeadingZero(inStr:String){
if(inStr.replace(/\d+/,"") != ""){
trace("DrDredelDate: stripLeadingZero: inStr " + inStr + " is non numeric. returning null");
return null;
}
inStr = inStr.replace(/^0+/,"");
if(inStr == "")
return 0;
return parseInt(inStr);
}
private function militaryToClockHour(){
if(hours == 0 || hours == 12)
return 12;
if(hours < 12)return hours;
return hours - 12;
}
private function AMPM(){
return (hours < 12)?"AM":"PM";
}
private function ampm(){
return (hours < 12)?"am":"pm";
}
}
}
包{
公共类DrDredelDate{
/*
通过传递初始化新日期:
a) 无-将生成具有当前时间的对象
b) 特定时间的毫秒字符串
c) 按以下两种方式之一格式化的时间:
2013-01-01 00:00:00-7:00——timzeone是可选的,如果省略,日期将创建为GMT
或
2012-01-18T23:30:42-08:00——时区非可选
*/
公共函数DrDredelDate(inD:String=null){
if(inD==null){
d=新日期();
getDateVars(d)
}
否则,如果(inD.replace(/\d+/,“”))=“”){
d=新日期(parseInt(inD))
getDateVars(d);
}
其他的
d=转换字符串(inD);
}
/**
使用以下控制字符设置格式:
m=月,
d=天,
yy=2位数年份,
yyyy=4位数的年份,
h=24小时时间,
M=分钟,
s=秒,
t=作为数字偏移的时区,
c=月份缩写,
W=工作日
H=12小时
a=上午/下午
A=上午/下午
T=时区缩写//如果可用
*/
公共函数getFormattedDate(formatString:String):String{
var-ret:String=“”;
对于(变量i=0;i比较数据时间)
}
公共函数isSameAsDate(comparisonDate:Date):布尔值{
返回(d.time==comparisonDate.time)
}
公共函数getTime(){
返回d.time;
}
公共函数getDate(){
返回d;
}
///////////下面没什么可看的////////////////
公共静态变量PST:String=“PST”;
公共静态变量EST:String=“EST”;
公共静态变量CST:String=“CST”;
公共静态变量MST:String=“MST”;
私有变量d:日期;
公共变量年、月、月、日、工作日、时、分、秒、时区偏移;
私有函数getDateVars(d:Date){
this.year=d.fullYear;
本月=d月;
this.date=d.date;
这个小时=d小时;
this.minutes=d.minutes;
这个秒=d秒;
this.timezoneOffset=d.timezoneOffset;
}
私有函数convertFromString(inD:String):日期{
//2012-10-03 10:15:00GMT-0800
if(inD.match(/\d{4}-\d{1,2}-\d{1,2}\d{1,2}\:\d{1,2}\:\d{1,2}/){
返回timeFromMySqlTimestamp(inD);
}
//2012-01-18T23:30:00-08:00
if(inD.match(/\d{4}-\d{1,2}-\d{1,2}T\d{1,2}\:\d{1,2}\:\d{1,2}/){
返回timeFromGenericTimestamp(inD);
}
返回null;
}
私有变量monthAbbr=['','一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'];
公共函数getWeekday(){
返回d.toString().split(“”[0];
}
公共函数getMonthAbbr(){
返回d.toString().split(“”[1];
}
私有函数timeFromMySqlTimestamp(inD:String):日期{
//2012-10-19 03:15:22太平洋标准时间
变量部分:Array=inD.split(“”;//0=date,1=hours,2=timezone
年份=带引线零(部分[0]。拆分(“-”[0]);
月份=带引线零(部分[0]。拆分(“-”[1]);
日期=带引线零(部分[0]。拆分(“-”[2]);
小时数=带引线零(部分[1]。拆分(“:”[0]);
分钟数=带引线零(部分[1]。拆分(“:”[1]);
秒=带引线零(部分[1]。拆分(“:”[2]);
试一试{
timezoneOffset=getTZFromString(第[2]部分);
}捕获(e:错误){
timezoneOffset=“GMT-000”;
}
返回新日期(monthAbbr[月]+“”+日期+“”+年+“”+小时+:“+分钟+”:“+秒+”“+时区偏移量);//09 03 2012 23:39:05 GMT-0500
}
私有var timezoneString=“”;
私有函数getTZFromString(inStr:String){
如果(inStr.toUpperCase().replace(/[^A-Z]/g)=“EST”){timezoneString=“EST”;返回“GMT-0500”};
如果(inStr.toUpperCase().replace(/[^A-Z]/g)==“EDT”){timezoneString=“EDT”;返回“GMT-0400”};
如果(inStr.toUpperCase().replace(/[^A-Z]/g)=“CST”){timezoneString=“CST”;返回“GMT-0600”};
如果(inStr.toUpperCase().replace(/[^A-Z]/g)=“CDT”){timezoneString=“CDT”;返回“GMT-0500”};