views:

93

answers:

2

so I have a variable containing a date object. I want to convert it to a string in this format: dd/mm/yyyy. How could this be achieved?

A: 

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;
 }
dhorn
+3  A: 

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.getDay()) + "/" +
     pad(date.getMonth() + 1) + "/" + 
     date.getFullYear();
}
Chandra Patni
This is way better than my answer. Listen to this man!
dhorn
@dhorn Thank you for your kind words.
Chandra Patni