0

API からこの日付/時刻を受け取りました:

2012-03-31 12:00:00

これを行う最善の方法は何ですか:

var date = new Date("2012-03-31 12:00:00")Firefox が無効な日付について不平を言うことはありませんか?

4

2 に答える 2

3

You can match all the fields in the date time string with:

var str = "2012-03-31 12:00:00";
var fields = str.match(/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/);

Now fields[1] contains the year, fields[2] the month, etc. Then you can call Date with:

// months are zero-based, so we have to subtract 1
var date = new Date(+fields[1], +fields[2] - 1, +fields[3], +fields[4], +fields[5], +fields[6]);

Or use a library like http://momentjs.com/ which does this for you.

于 2013-02-25T11:22:26.050 に答える
2

使用できる日付だけが必要な場合:

var date = new Date("2012-03-31 12:00:00".split(" ")[0]);

& firefox は文句を言いません。

于 2013-02-25T11:51:55.637 に答える