date format with adobe flex
I have a date and I would like to format the date like that: YYYYMMDD HHMMSS so for today it's 20100304 17300开发者_如何学运维0
But when I'm doing this:
var todayStr:String = today.getFullYear()+today.getMonth()+today.getDay()+today.getHours()+today.getMinutes();
the problem is for March getMonth() sent "3" and I would like "03". There is the same problem with the day.
How I can do?
Thanks.
You can use a DateFormatter. Have a look at this : Dateformatter @ adobe
var formatter : DateFormatter;
formatter = new DateFormatter();
formatter.formatString = "YYYYMMDD HHNNSS";
formatter.format(dateInstance);
beware of the 'N' used for the minutes
Try a simple function like:
private function formatTwoDigits(val:Number) : String {
var retVal:String = "";
retVal = (val > 9) ? val.toString() : "0" + val.toString();
return retVal;
}
Then use as:
var month:String = formatTwoDigits(today.getMonth()+1); // add 1 because months are zero-based
While there's probably an easier way you can just do this more procedurally.
var todayStr:String;
todayStr+=today.getFullYear();
if(today.getMonth()<10){
todayStr+="0";
todayStr+=today.getMonth();
}else{
todayStr+=today.getMonth();
}
etc.
精彩评论