How to parse a string in a date pattern using dojo

I have a value of '05/17/2010' dojo.date.locale '05/17/2010' I would like to get it as 'May 17, 2010' using dojo.date.locale . I tried using 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 does not give me the desired date pattern.

I also tried replacing the template with datePattern : "MMMM d, yyyy" , but it returns null .

+4
source share
3 answers

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

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

When you speak

 alert(x); 

which forces x to a string using the Date.toString () method, which is browser dependent, but will give you a result like what you got. - May 17, 2010 00:00:00 GMT-0500 (Central Daylight Time)

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

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

I'm not sure if this will work, although after your initial declaration 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 the value x was not initially set?

+1
source

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

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

Source: https://habr.com/ru/post/1310153/


All Articles