我想将时间戳转换为这种格式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
答案 0 :(得分:0)
修改了以下代码:
itm.timestamp = moment(+itm.timestamp).format("DD MMM YYYY hh:mm A");
+
符号将string
转换为integer
,然后可以将其传递到moment()
。然后使用format()
根据您的要求安排输出。
完整解决方案摘要:
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("DD MMM YYYY hh:mm A");
console.log(itm);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>