- var now = new Date(1539876689000).toLocaleString();
- console.log(now);
后台传来时间戳, 前台用 JS 可以处理得到具体时间
2. 正则表达式格式化时间
- replace(/\/{
- 1
- }/,'年') // 将第一个出现的 / 替换成 年
- .replace(/\/{
- 1
- }/,'月') // 将第二个出现的 / 替换成 月
- .replace(/ {
- 1
- }/,'日') // 将第一个出现的空格替换成日 + 空格
- .replace(/:\d{
- 1,2
- }$/,' ')// 将: 后出现的连续一到两个数字到结尾 (毫秒数) 替换成空格
3. 根据时间戳取时分秒, 年月日
- var now = new Date(1539876689000);
- var day = now.getUTCDate();
- var year = now.getUTCFullYear();
- var month = now.getMonth();
- var hour = now.getUTCHours();
- var min = now.getMinutes();
- //console.log(year,month,day,hour,min);
- str = year + "年" + month +"月" + day +"日" +"" + hour +" 时 "+ min +" 分 ";
- console.log(str);
来源: https://www.2cto.com/kf/201810/782621.html