views:

40

answers:

2

Hi ,

I was trying to convert this format of String to Date and was unsuccessfull

"23-DEC-2008" to a Date Object ,it looks like its not accepting "-" and i could see NULL in the date object after formatting .

Can somebody let me know if u have come across this problem .

Thanks , Sudeep

A: 

The "-" shouldn't be an issue. I've converted SQL timestamp strings to Date objects with no problem (the format of SQL date strings is YYYY-MM-DD). What is the format string you're using? Try using the format string "DD-MMM-YYYY" and see if that works.

Edit

Sorry, my solution only applies to the DateFormatter class from Flex, and not Actionscript. After looking at the documentation for the Actionscript Date class I saw the following:

The year month and day terms can be separated by a forward slash (/) or by spaces, but never by a dash (-). (1)

If you're stuck using straight Actionscript, it looks as if you'll have to write your own parse method that accepts the "-".

Dan
Hi Dan ,I removed the - and replaced it with - but still it doesnt accept DEC
kumar
HI DAN ,YOUR SOLUTION IS EXCELLENT ,ITS WORKING FINE AND IT WAS MY MISTAKE THAT I WRITTEN SOMETHINGS WRONG ..THANKS FOR THE HELP ..
kumar
Hey no problem, glad I could help!
Dan
A: 

this sort of works ...

public function parse(source:String):Date {
    var ret:Date = new Date(0, 0, 0, 0, 0, 0, 0);
    var parts:Array = source.split("-");
    ret.fullYear = Number(parts[2]);
    ret.setDate(Number(parts[0]));
    var month:int = "jan,feb,mar,apr,may,jun,jul,aug,sep,oct,now,dec".split(",").indexOf(String(parts[1]).toLowerCase());
    if (month == -1) throw "could not parse month";
    ret.setMonth(month);
    return ret;
}

but really, i don't like it ... if 'DEC' were 'Dec' , then

Date.parse("23-Dec-2008".split("-").join(" "))

would work ... but still ... i think, you should get something more robust ...

back2dos
you could say month.toLowerCase() to ensure that it would parse in that scenario.
Joel Hooks
nah, you couldn' really, because "23-dec-2008" won't work either ... -.- ... this is really nuts ... :D
back2dos
Hi,I have changed the logic like I have asked our backend guy to send a raw date and i have used label function and formatted it using label function for that particular column and achived it .I could not use the logic as the string is dynamic in the backend and could come in any way so I have used the said logic Thannks for the reply the above logic works fine and need to make it more robust
kumar