开发者

how to parse string into a date pattern using dojo

I have a value of '05/17/2010' I would like to get it as 'May 17, 2010' by using dojo.date.locale. 开发者_JAVA技巧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.


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'});


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?


The problem is you have to first create a Date object and then you can format it, because the format function takes, as its first parameter, a Date object and not a string. So, if you do the following, it will work fine:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜