解析日期时间的有效方法

时间:2018-06-06 15:38:57

标签: javascript

如果我有一个数组如下

var year = 2018;
var date = ["6/06", "03:00", "PM"];

如何有效地解析它以便我想做

var year = 2018;
var parsedDate = Date.UTC(year, 5, 6, 15, 0);

请注意,月份从零开始。

1 个答案:

答案 0 :(得分:-1)

对阵列的一些操作应该这样做:

let [daymonth, hourmin, ampm] = date;
let [day, month] = daymonth.split("/");
let [hour, min] = hourmin.split(":");
if(ampm === "PM") hour = +hour + 12;
const parsedDate = new Date(year, month - 1, +day, +hour, +min);