views:

204

answers:

2

Hi, I have a value as '05/17/2010' I would like to get it as May 17, 2010 by using dojo.date.locale.I have tried using the dojo.date.locale.parse as follows :

x = '05/17/2010' var x= dojo.date.locale.parse(x, {datePattern: "MM/dd/yyyy", selector: "date"}); alert(x)

This doesnt give me the desired date pattern

I also tried replacing the pattern as datePattern : "MMMM d, yyyy" but it returns me null.

Any help highly appreciated.

Thanks

A: 

I'm not sure if this works - though after your initial declaration of x, there is no semicolon before setting it a second time. I broke your code into three lines:

var x = '05/17/2010';
x = dojo.date.locale.parse(x, {datePattern: "MM/dd/yyyy", selector: "date"});
alert (x);

Perhaps it was just a matter of x not being set initially?

JYelton
well...adddng a semicolon doesn't work either , since javascript is loosely typed
Dojouser
And also alerting x gives me the output like this Mon May 17 2010 00:00:00 GMT-0500 (Central Daylight Time)but this is not what i want ... I would like to get it in May 17, 2010 format.If I try changing the datepattern to MMMM d, yyyy, the alert gives me null . Any ideas as to what is going wrong ?
Dojouser
You were definitely missing a semi-colon after the first assignment of x. That's just a syntax issue and nothing to do with typing. You also shouldn't use var after assigning x, though the extra declaration will just be ignored. JYelton's syntax looks correct.
peller
It sounds like progress. Instead of getting `null` you are now getting the system default date format (as converted from a java date object). Now you just need to convert that date into the desired format string.
JYelton
+1  A: 

dojo.date.locale.parse takes a formatted string and returns a Javascript Date object.

var x = dojo.date.locale.parse('05/17/2010', {datePattern: "MM/dd/yyyy", selector: "date"});

When you say

alert(x);

that coerces x to a string using the Date.toString() method, which varies by browser, but will give you output like what you got -- Mon May 17 2010 00:00:00 GMT-0500 (Central Daylight Time)

If you want to then format the date a special way, pass the result of your parse to dojo.date.locale.format with a specific date format:

var y = dojo.date.locale.format(x, {datePattern:"MMMM d, yyyy", selector: 'date'});
peller