Khattab
Khattab

Reputation: 737

How to get the Current Date Time Formatted in Flex

I'm trying to get the current date time in Flex/AIR?

Upvotes: 9

Views: 37392

Answers (3)

Khattab
Khattab

Reputation: 737

To get the current date time, just create a new Date object with no values into the constructor, like this:

    var CurrentDateTime:Date = new Date();

Formatting it depends on how you want to format it; here is one option:

private function CurrentDateTimeString():String
{               
    var CurrentDateTime:Date = new Date();
    var CurrentDF:DateFormatter = new DateFormatter();
    CurrentDF.formatString = "MM/DD/YY LL:NN:SS A"
    var DateTimeString:String = CurrentDF.format(CurrentDateTime);
    return DateTimeString;
}

Upvotes: 26

Khattab
Khattab

Reputation: 737

private function CurrentDateTimeString():String
{

   var CurrentDateTime:Date = new Date();


   var DateString:String = CurrentDateTime.getMonth().toString()+ "/"+CurrentDateTime.getDate().toString() +"/"+CurrentDateTime.getFullYear().toString();
   var TimeString:String = CurrentDateTime.getHours().toString()+ ":"+ doubleDigitFormat(CurrentDateTime.getMinutes());
   var DateTimeString:String = DateString + " " + TimeString;
   return DateTimeString;
  }

function doubleDigitFormat(num:uint):String 
{

   if(num < 10) {
    return ("0" + num);
   }
   return num.toString();

  }

Upvotes: 3

Related Questions