如何更改数组中所有项目的日期格式
How to change date format for all the items in an array
我有数组 objproduct
,它有一个字段 lastseen
,类型 Date
,格式 DD/MM/YYYY h:mm:ss a
。我需要将此日期格式更改为 MM/DD/YYYY h:mm:ss a
.
我试过了,但看起来我的查询不正确
objproduct.forEach(element => {
moment(element.lastseen,'DD/MM/YYYY h:mm:ss a').format('MM/DD/YYYY h:mm:ss a');
});
这是我的示例数组
{
"lastseen":"DD/MM/YYYY h:mm:ss a"
"org": "EDC",
"Id": 816,
},
{
"lastseen":"DD/MM/YYYY h:mm:ss a"
"org": "AXC",
"Id": 85427,
}
我正在寻找这个输出
{
"lastseen":"MM/DD/YYYY h:mm:ss a"
"org": "EDC",
"Id": 816,
},
{
"lastseen":"MM/DD/YYYY h:mm:ss a"
"org": "AXC",
"Id": 85427,
}
我怎样才能让它发挥作用?
在你的循环中,你正在创建一个新的时刻对象,然后调用它的 .format()
方法来 return 一个字符串,但是,你从未真正使用过这个 returned字符串值。这就像编写一个计算结果为一个值的表达式,但该值从未实际使用过:
objproduct.forEach(element => {
1 + 2; // Results in 3, but nothing ever happens with this number
});
以上想法正在您的代码中发生。要使用该值,您需要设置元素的 lastseen
属性 来更新它:
objproduct.forEach(element => {
element.lastseen = moment(element.lastseen,'DD/MM/YYYY h:mm:ss a').format('MM/DD/YYYY h:mm:ss a');
});
我有数组 objproduct
,它有一个字段 lastseen
,类型 Date
,格式 DD/MM/YYYY h:mm:ss a
。我需要将此日期格式更改为 MM/DD/YYYY h:mm:ss a
.
我试过了,但看起来我的查询不正确
objproduct.forEach(element => {
moment(element.lastseen,'DD/MM/YYYY h:mm:ss a').format('MM/DD/YYYY h:mm:ss a');
});
这是我的示例数组
{
"lastseen":"DD/MM/YYYY h:mm:ss a"
"org": "EDC",
"Id": 816,
},
{
"lastseen":"DD/MM/YYYY h:mm:ss a"
"org": "AXC",
"Id": 85427,
}
我正在寻找这个输出
{
"lastseen":"MM/DD/YYYY h:mm:ss a"
"org": "EDC",
"Id": 816,
},
{
"lastseen":"MM/DD/YYYY h:mm:ss a"
"org": "AXC",
"Id": 85427,
}
我怎样才能让它发挥作用?
在你的循环中,你正在创建一个新的时刻对象,然后调用它的 .format()
方法来 return 一个字符串,但是,你从未真正使用过这个 returned字符串值。这就像编写一个计算结果为一个值的表达式,但该值从未实际使用过:
objproduct.forEach(element => {
1 + 2; // Results in 3, but nothing ever happens with this number
});
以上想法正在您的代码中发生。要使用该值,您需要设置元素的 lastseen
属性 来更新它:
objproduct.forEach(element => {
element.lastseen = moment(element.lastseen,'DD/MM/YYYY h:mm:ss a').format('MM/DD/YYYY h:mm:ss a');
});