如何将字符串转换为JavaScript日期对象?
var st = "date in some format"
var dt = new Date();
var dt_st = // st in Date format, same as dt.
如何将字符串转换为JavaScript日期对象?
var st = "date in some format"
var dt = new Date();
var dt_st = // st in Date format, same as dt.
当前回答
var date = new Date(year, month, day);
or
var date = new Date('01/01/1970');
格式为“01-01-1970”的日期字符串在FireFox中行不通,所以最好在日期格式字符串中使用“/”而不是“-”。
其他回答
我已经创建了parseDateTime函数将字符串转换为日期对象,它在所有浏览器(包括IE浏览器)中工作,检查是否有人需要,引用 https://github.com/Umesh-Markande/Parse-String-to-Date-in-all-browser
function parseDateTime(datetime) {
var monthNames = [
"January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December"
];
if(datetime.split(' ').length == 3){
var date = datetime.split(' ')[0];
var time = datetime.split(' ')[1].replace('.00','');
var timearray = time.split(':');
var hours = parseInt(time.split(':')[0]);
var format = datetime.split(' ')[2];
var bits = date.split(/\D/);
date = new Date(bits[0], --bits[1], bits[2]); /* if you change format of datetime which is passed to this function, you need to change bits e.x ( bits[0], bits[1], bits[2 ]) position as per date, months and year it represent bits array.*/
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();
if ((format === 'PM' || format === 'pm') && hours !== 12) {
hours += 12;
try{ time = hours+':'+timearray[1]+':'+timearray[2] }catch(e){ time = hours+':'+timearray[1] }
}
var formateddatetime = new Date(monthNames[monthIndex] + ' ' + day + ' ' + year + ' ' + time);
return formateddatetime;
}else if(datetime.split(' ').length == 2){
var date = datetime.split(' ')[0];
var time = datetime.split(' ')[1];
var bits = date.split(/\D/);
var datetimevalue = new Date(bits[0], --bits[1], bits[2]); /* if you change format of datetime which is passed to this function, you need to change bits e.x ( bits[0], bits[1], bits[2 ]) position as per date, months and year it represent bits array.*/
var day = datetimevalue.getDate();
var monthIndex = datetimevalue.getMonth();
var year = datetimevalue.getFullYear();
var formateddatetime = new Date(monthNames[monthIndex] + ' ' + day + ' ' + year + ' ' + time);
return formateddatetime;
}else if(datetime != ''){
var bits = datetime.split(/\D/);
var date = new Date(bits[0], --bits[1], bits[2]); /* if you change format of datetime which is passed to this function, you need to change bits e.x ( bits[0], bits[1], bits[2 ]) position as per date, months and year it represent bits array.*/
return date;
}
return datetime;
}
var date1 = '2018-05-14 05:04:22 AM'; // yyyy-mm-dd hh:mm:ss A
var date2 = '2018/05/14 05:04:22 AM'; // yyyy/mm/dd hh:mm:ss A
var date3 = '2018/05/04'; // yyyy/mm/dd
var date4 = '2018-05-04'; // yyyy-mm-dd
var date5 = '2018-05-14 15:04:22'; // yyyy-mm-dd HH:mm:ss
var date6 = '2018/05/14 14:04:22'; // yyyy/mm/dd HH:mm:ss
console.log(parseDateTime(date1))
console.log(parseDateTime(date2))
console.log(parseDateTime(date3))
console.log(parseDateTime(date4))
console.log(parseDateTime(date5))
console.log(parseDateTime(date6))
**Output---**
Mon May 14 2018 05:04:22 GMT+0530 (India Standard Time)
Mon May 14 2018 05:04:22 GMT+0530 (India Standard Time)
Fri May 04 2018 00:00:00 GMT+0530 (India Standard Time)
Fri May 04 2018 00:00:00 GMT+0530 (India Standard Time)
Mon May 14 2018 15:04:22 GMT+0530 (India Standard Time)
Mon May 14 2018 14:04:22 GMT+0530 (India Standard Time)
还有另一种方式:
String.prototype.toDate = function(format) {
format = format || "dmy";
var separator = this.match(/[^0-9]/)[0];
var components = this.split(separator);
var day, month, year;
for (var key in format) {
var fmt_value = format[key];
var value = components[key];
switch (fmt_value) {
case "d":
day = parseInt(value);
break;
case "m":
month = parseInt(value)-1;
break;
case "y":
year = parseInt(value);
}
}
return new Date(year, month, day);
};
a = "3/2/2017";
console.log(a.toDate("dmy"));
// Date 2017-02-03T00:00:00.000Z
只是新日期(st);
假设这是正确的格式。
转换为pt-BR格式:
var dateString = "13/10/2014";
var dataSplit = dateString.split('/');
var dateConverted;
if (dataSplit[2].split(" ").length > 1) {
var hora = dataSplit[2].split(" ")[1].split(':');
dataSplit[2] = dataSplit[2].split(" ")[0];
dateConverted = new Date(dataSplit[2], dataSplit[1]-1, dataSplit[0], hora[0], hora[1]);
} else {
dateConverted = new Date(dataSplit[2], dataSplit[1] - 1, dataSplit[0]);
}
我希望能帮助到别人!!
如果要从“dd/MM/yyyy”格式转换。这里有一个例子:
var pattern = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
var arrayDate = stringDate.match(pattern);
var dt = new Date(arrayDate[3], arrayDate[2] - 1, arrayDate[1]);
此解决方案适用于IE 9以下版本。