将日期时间转换为有效的 JavaScript 日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6695600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:51:28  来源:igfitidea点击:

Convert datetime to valid JavaScript date

javascriptdatetime

提问by Jeremy B.

I have a datetime string being provided to me in the following format:

我以以下格式向我提供了一个日期时间字符串:

yyyy-MM-dd HH:mm:ss
2011-07-14 11:23:00

When attempting to parse it into a JavaScript date()object it fails. What is the best way to convert this into a format that JavaScript can understand?

尝试将其解析为 JavaScriptdate()对象时失败。将其转换为 JavaScript 可以理解的格式的最佳方法是什么?

The answers below suggest something like

下面的答案表明类似

var myDate = new Date('2011-07-14 11:23:00');

Which is what I was using. It appears this may be a browser issue. I've made a http://jsfiddle.net/czeBu/for this. It works OK for me in Chrome. In Firefox 5.0.1 on OS X it returns Invalid Date.

这就是我正在使用的。看来这可能是浏览器问题。我为此做了一个http://jsfiddle.net/czeBu/。它在Chrome 中对我有用。在 OS X 上的 Firefox 5.0.1 中,它返回Invalid Date.

回答by mplungjan

This works everywhere including Safari 5 and Firefox 5 on OS X.

这适用于任何地方,包括 OS X 上的 Safari 5 和 Firefox 5。

UPDATE: Fx Quantum (54) has no need for the replace, but Safari 11 is still not happy unless you convert as below

更新:Fx Quantum (54) 不需要替换,但 Safari 11 仍然不满意,除非您按如下方式转换

var date_test = new Date("2011-07-14 11:23:00".replace(/-/g,"/"));
console.log(date_test);



FIDDLE

小提琴

回答by Shekhar Patel

One can use the getmonth and getday methods to get only the date.

可以使用 getmonth 和 getday 方法仅获取日期。

Here I attach my solution:

在这里我附上我的解决方案:

var fullDate = new Date(); console.log(fullDate);
var twoDigitMonth = fullDate.getMonth() + "";
if (twoDigitMonth.length == 1)
    twoDigitMonth = "0" + twoDigitMonth;
var twoDigitDate = fullDate.getDate() + "";
if (twoDigitDate.length == 1)
    twoDigitDate = "0" + twoDigitDate;
var currentDate = twoDigitDate + "/" + twoDigitMonth + "/" + fullDate.getFullYear(); console.log(currentDate);

回答by pimvdb

Just use Date.parse()which returns a Number, then use new Date()to parse it:

只需使用Date.parse()它返回一个数字,然后使用new Date()它来解析它:

var thedate = new Date(Date.parse("2011-07-14 11:23:00"));

回答by Satyam Naolekar

Use:

用:

enter code var moment = require('moment')
var startDate = moment('2013-5-11 8:73:18', 'YYYY-M-DD HH:mm:ss')

Moment.js works very well. You can read more about it here.

Moment.js 工作得很好。您可以在此处阅读更多相关信息。

回答by Solution Spirit

You can use moment.js for that, it will convert DateTime object into valid Javascript formated date:

您可以为此使用 moment.js,它会将 DateTime 对象转换为有效的 Javascript 格式日期:

   moment(DateOfBirth).format('DD-MMM-YYYY'); // put format as you want 

   Output: 28-Apr-1993

Hope it will help you :)

希望它会帮助你:)

回答by Ragesh Punathil

function ConvertDateFromDiv(divTimeStr) {
    //eg:-divTimeStr=18/03/2013 12:53:00
    var tmstr = divTimeStr.toString().split(' '); //'21-01-2013 PM 3:20:24'
    var dt = tmstr[0].split('/');
    var str = dt[2] + "/" + dt[1] + "/" + dt[0] + " " + tmstr[1]; //+ " " + tmstr[1]//'2013/01/20 3:20:24 pm'
    var time = new Date(str);
    if (time == "Invalid Date") {
        time = new Date(divTimeStr);
    }
    return time;
}

回答by Muhafil Saiyed

You can use get methods:

您可以使用获取方法:

var fullDate = new Date();
console.log(fullDate);
var twoDigitMonth = fullDate.getMonth() + "";
if (twoDigitMonth.length == 1)
    twoDigitMonth = "0" + twoDigitMonth;
var twoDigitDate = fullDate.getDate() + "";
if (twoDigitDate.length == 1)
    twoDigitDate = "0" + twoDigitDate;
var currentDate = twoDigitDate + "/" + twoDigitMonth + "/" + fullDate.getFullYear(); console.log(currentDate);

回答by Harmen

new Date("2011-07-14 11:23:00");works fine for me.

new Date("2011-07-14 11:23:00");对我来说很好用。