Javascript 如何在javascript中将字符串'dd/mm/yy hh:MM:ss'转换为日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30891884/
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 05:53:57  来源:igfitidea点击:

How To Convert String ' dd/mm/yy hh:MM:ss ' to Date in javascript?

javascriptjquerydatedatetime

提问by Kumar

I have Done this

我已经这样做了

    var d='dd/mm/yy hh:MM:ss';
    var d1=d.split(" ");
    var date=d1[0].split("/");
    var time=d1[1].split(":");
    var dd=date[0];
    var mm=date[1]-1;
    var yy=date[2];
    var hh=time[0];
    var min=time[1];
    var ss=time[2];
    var fromdt= new Date("20"+yy,mm-1,dd,hh,min,ss);

Is there Any way to do it using JQuery OR JavaScript?

有没有办法使用 JQuery 或 JavaScript 来做到这一点?

回答by ANR Upgraded Version

If you are looking for alternatives in jquery or Javascript , then you can go with Moment.js,where you can Parse, Validate, Manipulate, and Displaydates in JavaScript.

如果您正在寻找 jquery 或 Javascript 的替代方案,那么您可以使用Moment.js,您可以在其中使用 JavaScript解析验证操作显示日期。

example:

例子:

  var date= moment("06/06/2015 11:11:11").format('DD-MMM-YYYY');

回答by marekful

How about Date.parse()?

如何Date.parse()

new Date( Date.parse("05/12/05 11:11:11") );
// Thu May 12 2005 11:11:11 GMT+0200 (CEST)

The output produced is in local timezone and will differ in browsers in different timezones.

生成的输出在本地时区,并且在不同时区的浏览器中会有所不同。

回答by Palani Kumar

We can convert any local date format to datetime datatype using moment js.

我们可以使用 moment js 将任何本地日期格式转换为日期时间数据类型。

Syntax:

句法:

moment('<local date value>','<local date format>').format('<expected convert format>')

Example:

例子:

moment('26/05/1986 00:00', 'DD/MM/YYYY HH:mm').format("MM/DD/YYYY HH:mm");

then the output will be 05/26/1986 00:00

那么输出将是05/26/1986 00:00

Cheers

干杯

回答by Sean Coley

This will work regardless of timezone for the format dd/mm/yyy hh:mm:ssonly. It also does not rely on third party packages:

这将dd/mm/yyy hh:mm:ss仅适用于格式的时区。它也不依赖第三方软件包:

let dtStr = "12/03/2010 09:55:35"

console.log(strToDate(dtStr))

// expected output:
// Fri Mar 12 2010 09:55:35

function strToDate(dtStr) {

    let dateParts = dtStr.split("/");
    let timeParts = dateParts[2].split(" ")[1].split(":");
    dateParts[2] = dateParts[2].split(" ")[0];
    // month is 0-based, that's why we need dataParts[1] - 1
    return dateObject = new Date(+dateParts[2], dateParts[1] - 1, +dateParts[0], timeParts[0], timeParts[1], timeParts[2]); 

}

回答by PhilVarg

Date#parseshould be able to parse that if you split on a string. Id also recommend looking into the npm package moment for date manipulation

Date#parse如果在字符串上拆分,应该能够解析它。我还建议查看 npm 包时刻以进行日期操作

回答by sAcH

From your code it seems that you are trying to convert string into date and also you are trying to fetch previous month. If yes then you can reconstruct your code as below:

从您的代码看来,您似乎正在尝试将字符串转换为日期,并且您还试图获取上个月的数据。如果是,那么您可以按如下方式重建您的代码:

Date.prototype.SubtractMonth = function(numberOfMonths) {
var d = this;
d.setMonth(d.getMonth() - numberOfMonths);
return d;
}

$(document).ready(function() {
     var dateString='2015-06-17T18:30:12';
    var d = new Date(dateString);
    alert(d.SubtractMonth(1));
});