How to convert a date object to a formatted string?
so I have a variable containing a date object. I want to convert it to开发者_如何学C a string in this format: dd/mm/yyyy. How could this be achieved?
You can use Flex 3.5 DateFormatter to format the date.
var fmt:DateFormatter = new DateFormatter();
fmt.formatString = "DD/MM/YYYY";
return fmt.format(date);
Or you can write your own:
function format(date:Date):String {
function pad(n:int):String {
return return n<10 ? '0'+n : n;
}
return pad(date.getDate()) + "/" +
pad(date.getMonth() + 1) + "/" +
date.getFullYear();
}
toDateString() might do what you need, but this should also work:
Straight from here
function dateToMMDDYYYY(aDate:Date):String {
var SEPARATOR:String = "/";
var mm:String = (aDate.month + 1).toString();
if (mm.length < 2) mm = "0" + mm;
var dd:String = aDate.date.toString();
if (dd.length < 2) dd = "0" + dd;
var yyyy:String = aDate.fullYear.toString();
return dd + SEPARATOR + mm + SEPARATOR + yyyy;
}
精彩评论