[英]How to convert timestamp to date string?
如何将api返回的时间戳值(2021170519)转换为格式为HH:SS DD.MM.YY(20:21 17.05.19)的日期字符串?
If the data is unified and numbers are padded you can just access their indexes. 如果数据是统一的且已填充数字,则可以访问它们的索引。
var data = "2021170519"; var hh = data[0] + data[1]; var mm = data[2] + data[3]; var dd = data[4] + data[5]; var MM = data[6] + data[7]; var yyyy = data[8] + data[9]; console.log(hh + ":" + mm + " " + dd + "." + MM + "." + yyyy);
您可以使用moment.js库并根据需要设置日期格式。
moment(timestamp).format('hh:ss dd.MM.YY');
If the format is consistent, you can dissect the string into smaller chunks with a regular expression: 如果格式一致,则可以使用正则表达式将字符串分解为较小的块:
var str = '2021170519' str = str.match(/.{1,2}/g) const newStr = `${str[0]}:${str[1]} ${str[2]}.${str[3]}.${str[4]}` console.log(newStr)
I would recommend using a library like moment.js, however. 但是,我建议使用像moment.js这样的库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.