tags:

views:

1277

answers:

1

I have a String in the format "20-Aug-2008". I want this to be converted to either 20/08/2008 or 08/20/2008? How can I do this?

I just want to remove all those hyphens from the String and convert it to a date value. The dateFormatter function accepts only date values in the format mm/dd/yyyy.

Can someone help me out..

I used regex and removed all those hyphens,from the String. Now in an array I have the values, 20,Aug and 2008. How to proceed after that to convert to 20/08/2008?

Edit

  [Bindable]public var myDate:Date;

  public function init():void
  {
        var dateStr:String="20-Aug-2008";

        var rex:RegExp = /-/;

         var dateArray:Array = dateStr.split(rex);

   myDate= new Date(dateArray[0],dateArray[1],dateArray[2]);

  }

  <mx:DateFormatter id="DateDisplay" formatString="MM/DD/YYYY"/>

  <mx:TextArea id="date" text="{DateDisplay.format(myDate)}"/>

The value I get in the text area is: 01/00/NaN. Where have I gone wrong?

+1  A: 

I don't have a compiler handy (so I can't check this), but isn't the Date constructor expecting Year, Month (zero-based), Day? So the following should work:

myDate = new Date(dateArray[2], dateArray[0] - 1, dateArray[1]);

(Of course, you'll probably want to do some error-checking beforehand.)

From: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Date.html#includeExamplesSummary

EDIT: Better links
http://livedocs.adobe.com/flex/2/langref/Date.html#Date%28%29
http://www.darronschall.com/weblog/2006/12/actionscript-30-tip-date-constructor.cfm

EDIT 2: Oops. Forgot that you were dealing with a Month string, not a number. You could do something like the following, instead:

function getMonth(monthString:String):int
{
    var months = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');

    for (var i:int=0; i<months.length; i++)
    {
        if months[i] == monthString)
        {
          return i;
        }
    }

    // not a valid month string
    return -1;
}

Then, use getMonth to find the number that would be associated with the given month name. Again, I don't have a compiler handy, so this may not be right. Also, there's got to be an easier way to do this...just can't think of one right now.

Michael Todd
I have dateArray[1] as Aug. How to change that to a number?Coz this Date constructor works only if I give a number. But I have the value as Aug, Is there any method to change it to a number?
Angeline Aarthi
thank you... I too was searching if there is any function that gives the number of the month,if the month string is passed. Didn't get a function like that. So for now, gonna use this function. THanks for helping me out. :-)
Angeline Aarthi