how to parse string into a date pattern using dojo
date, dojo, javascript
Solution
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'});
Problem
I have a value of `'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 doesn't give me the desired date pattern. I also tried replacing the pattern as `datePattern : "MMMM d, yyyy"` but it returns `null`.