我想将 20170603 转换为日期。我想使用函数 new Date(2017,06,03),但如何从日期中提取数字?我尝试使用
new Date(parseInt(startDate.substr(0,4)), parseInt(startDate.substr(5,6)) -1, parseInt(startDate.substr(6,8)))
但是,这给了我非常奇怪的值。如何将准确的数字提取到 Date 函数中?
最佳答案
substr
函数的第二个参数是要提取的字符串长度;
new Date(parseInt(startDate.substr(0,4)),
parseInt(startDate.substr(4,2)) - 1,
parseInt(startDate.substr(6,2)));
关于javascript - 如何将没有分隔符的字符串转换为日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45094035/