简体   繁体   中英

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 173000

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM