我有类似日期的字符串
“新日期(0,0,0,11,13,16)”
,并将其更改为
新日期(0,0,0,11,13,16)
任何人都有想法。
谢谢
答案 0 :(得分:3)
var str = 'new Date(0,0,0,11,13,16)';
var str1 = str.match(/\(.*\)/g)[0];
str1 = str1.replace('(', '');
str1 = str1.replace(')', '');
var dateArr = str1.split(',');
var updatedDate = new
Date(dateArr[0],dateArr[1],dateArr[2],dateArr[3],dateArr[4],dateArr[5]);
console.log(updatedDate);
答案 1 :(得分:1)
使用正则表达式通过仅匹配数字来解决此问题。 match
将返回数字数组,因此请使用spread operator将所有参数设置为Date。
const res = new Date(...'new Date(0,0,0,11,13,16)'.match(/[0-9]+/g));
console.log(res);
答案 2 :(得分:0)
理论上,您可以使用eval
函数。
根据使用情况,确实会带来一些安全风险。了解有关此here
的更多信息如果可能的话,我建议您使用其他形式的日期字符串,例如“ yyyy-mm-dd”(2019-02-17)并使用new Date(dateString) constructor
(new Date('2019-01-17')
)解析为日期对象。