开发者

date format with adobe flex

开发者 https://www.devze.com 2022-12-22 02:50 出处:网络
I have a date and I would like to format the date like that: YYYYMMDD HHMMSS so for today it\'s 20100304 17300开发者_如何学运维0

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消