【发布时间】:2020-04-12 18:15:28
【问题描述】:
我想将时间戳转换成这种格式20 dec 2019 10:10 AM。
我有一个对象数组,所以我已经实现了这个代码
var data =[
{
report: 'xyz',
office: 'ccc.xbx',
timestamp: 1576023365640,
officeId: 'ttttttt',
month: 3,
year: 2019
},
{
report: 'yyy',
totalUsers: 1288,
office: 'yyyy.in',
timestamp: 1576023365640,
officeId: 'prfzIYlOkAw3gqc03yszttffffdddaq',
date: 24,
month: 10,
year: 2019
},
{
report: 'xyz',
office: 'ccc.xbx',
timestamp: 1576023365640,
officeId: 'DZxUwW9TzbwCYNclYLgE',
month: 3,
year: 2019
},
{
report: 'xyz',
office: 'ccc.xbx',
timestamp: 1576023365640,
officeId: 'DZxUwW9TzbwCYNclYLgE',
month: 3,
year: 2019
}
]
data.forEach(function(itm) {
itm.timestamp = moment(itm.timestamp).format("LLL");
console.log(itm);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
特定时间戳的这种格式的预期输出
20 dec 2019 10:10 AM
【问题讨论】:
-
moment(+itm.timestamp).format("LLL");不要使用字符串时间戳,必须是int,输出格式December 11, 2019 5:46 AM -
只需使用
.format('DD MMM YYYY hh:mm A')。 -
@siddhantsankhe——时间戳是一个数字。它不必是整数。
-
@RobG 在编辑此问题之前,时间戳存储为
string,其格式将为"Invalid Date"
标签: javascript date timestamp