admin管理员组

文章数量:1287219

The language of my OS (Windows) is in danish, and so is the language of my browsers.

When i am trying to parse a Date in the danish format (dd-MM-yyyy) like this:

var x = "18-08-1989"
var date = new Date(x);

I get the wrong date from javascript (i want 18'th of August 1989). When i transform this string to english, and parse it, it returns the correct date.

Does the format of the date string always have to be: yyyy-MM-dd when using the JS Date object??

The language of my OS (Windows) is in danish, and so is the language of my browsers.

When i am trying to parse a Date in the danish format (dd-MM-yyyy) like this:

var x = "18-08-1989"
var date = new Date(x);

I get the wrong date from javascript (i want 18'th of August 1989). When i transform this string to english, and parse it, it returns the correct date.

Does the format of the date string always have to be: yyyy-MM-dd when using the JS Date object??

Share Improve this question asked May 2, 2013 at 12:55 KenciKenci 4,88216 gold badges69 silver badges113 bronze badges 3
  • "18-08-89" is not in the "dd-MM-yyyy" format, have you tried "18-08-1989"? – darma Commented May 2, 2013 at 12:57
  • blog.stevenlevithan./archives/date-time-format – Ajinkya Commented May 2, 2013 at 12:58
  • I corrected my question, and yes I have tried it – Kenci Commented May 2, 2013 at 12:59
Add a ment  | 

2 Answers 2

Reset to default 8

In basic use without specifying a locale, a formatted string in the default locale and with default options is returned.

var date = new Date(Date.UTC(2012, 11, 12, 3, 0, 0));

// toLocaleString without arguments depends on the implementation,
// the default locale, and the default time zone
date.toLocaleString();
// "12/11/2012, 7:00:00 PM" if run in en-US locale with time zone America/Los_Angeles

Using locales

var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));

// formats below assume the local time zone of the locale;
// America/Los_Angeles for the US

// US English uses month-day-year order
alert(date.toLocaleString("en-US"));
// "12/19/2012, 7:00:00 PM"

// British English uses day-month-year order
alert(date.toLocaleString("en-GB"));
// "20/12/2012 03:00:00"

// Korean uses year-month-day order
alert(date.toLocaleString("ko-KR"));
// "2012. 12. 20. 오후 12:00:00"

Look here form more info about Date object: https://developer.mozilla/en-US/docs/JavaScript/Reference/Global_Objects/Date

And here about format: https://www.rfc-editor/rfc/rfc2822#page-14

本文标签: jqueryWhat culture does the javascript Date object useStack Overflow