我有一个如下所示的dateTime字符串:
"1396-12-22 12:33:42"
如何将每个日期编号导出为单个int,如下所示:
1396
12
22
答案 0 :(得分:2)
您可以使用String.prototype.split
和destructuring assignment(如果使用ES6)来执行此操作:
let dateTimeString = "1396-12-22 12:33:42"
let dateString = dateTimeString.split(' ')[0];
let [ year, month, day ] = dateString.split('-');
如果使用ES5,则代码为:
var dateTimeString = "1396-12-22 12:33:42"
var dateArray = dateTimeString.split(' ')[0].split('-');
var year = dateArray[0];
var month = dateArray[1];
var day = dateArray[2];
答案 1 :(得分:2)
var res = new Date("1396-12-22 12:33:42")
console.log(res.getFullYear()); // 1396
console.log(res.getMonth() + 1); // 12
console.log(res.getDate()); // 22
console.log(res.getHours()); // 12
console.log(res.getMinutes()); // 33
console.log(res.getSeconds()); // 42
答案 2 :(得分:1)
使用正则表达式:
id | item_num | other_col | description
A123 | 1 | lorem ipsum | Mary Had a...
A123 | 2 | dolor sit | ...little lamb.
B456 | 1 | lorem ipsum | ...Its fleece...
给出:
"1396-12-22 12:33:42".split(/\D+/)
答案 3 :(得分:1)
const dateTimeString = "1396-12-22 12:33:42";
const dateNumbers = dateTimeString
.substring(0, dateTimeString.indexOf(" "))
.split("-")
.map(value => parseInt(value));
console.log(dateNumbers);