一种方法是使用年、月和日作为参数 new Date
新日期(年,月[,日[,小时[,分钟[,秒[,毫秒]]]]]]);
您可以使用函数来准备日期字符串。
注意:月份是 0-11,这就是为什么 m-1
这是一个片段:
function prepareDate(d) {
[d, m, y] = d.split("-"); //Split the string
return [y, m - 1, d]; //Return as an array with y,m,d sequence
}
let str = "25-12-2017";
let d = new Date(...prepareDate(str));
console.log(d.getTime());Run Code Online (Sandbox Code Playgroud)
文档:https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date