js中时间和字符串的相互转化
首先是时间转化为字符串
date2str (date, split) {
let monthNames = [
"01", "02", "03",
"04", "05", "06", "07",
"08", "09", "10",
"11", "12"
];
let day = date.getDate()
if(day < 10) day = '0' + day
let monthIndex = date.getMonth()
let year = date.getFullYear()
return year + split + monthNames[monthIndex] + split + day;
}
然后是字符串转换为时间格式
str2date (str, split) {
let astr = str.split(split);
let ret = new Date();
if (astr.length >= 3) {
ret.setFullYear(parseInt(astr[0], 10));
ret.setMonth(parseInt(astr[1], 10) - 1);
ret.setDate(parseInt(astr[2], 10))
}
return ret;
}
上述的split
都表示分隔时间的字符串。
如果您觉得本文对您有用,欢迎捐赠或留言~
- 本博客所有文章除特别声明外,均可转载和分享,转载请注明出处!
- 本文地址:https://www.leevii.com/?p=690