我的约会是这样的:
19/août/2016 (août = august)我有下面的函数将月份转换成一个数字:
function swapMonthForNumber(str:String):String {
//do the same line of code for every item in the array
for(var i:int=0;i<months.length;i++){
//i is the item, which is 0 based, so we have to add 1 to make the right month number
str = str.replace(months[i],String(+i+1));
}
//return the updated string
return str;
}
str = swapMonthForNumber(mySharedObject.data.theDate);
trace("Php will use this date :"+str);因此str将为19/8/2016,但我希望str为19/08/2016 (在8之前添加0 )。
我该怎么做呢?
发布于 2016-08-20 03:33:26
查看Date类的参考资料!
如果忘记提到这个链接:flash.globalization.DateTimeFormatter
DateTimeFormatter(requestedLocaleIDName:String, dateStyle:String = "long", timeStyle:String = "long")下面是一个例子。
import flash.globalization.DateTimeFormatter;
var df:DateTimeFormatter = new DateTimeFormatter(LocaleID.DEFAULT, DateTimeStyle.SHORT, DateTimeStyle.NONE);
var currentDate:Date = new Date(2016,7,19);
var shortDate:String = df.format(currentDate);
trace (shortDate);
// output : 19/08/2016发布于 2016-08-19 07:25:24
将前导零添加到数字通常称为补零。
下面是执行此操作的函数,来自答案here。
公共函数zeroPad(number:int,width:int):String { var ret:String = ""+number;while( ret.length < width ) ret="0“+ ret;return ret;}
在swapMonthForNumber函数的for循环中,将代码替换为:
var month = zeroPad(i + 1, 2);
str = str.replace(months[i], month);https://stackoverflow.com/questions/39028411
复制相似问题