从 api 响应中拆分日期和时间

Split date and time from api response

我从 api 收到一系列响应。下面是响应数组。

[{createdBy:"user1",updatedDttm:
"2022-01-20T07:31:35.544Z"},
createdBy:"user2", updatedDttm:
"2022-02-20T09:31:37.544Z"}]

从上面的回复中我想拆分“updatedDttm” (日期和时间)为每个用户并将其保存到与“日期”、“时间”相同的数组中,如下所示。

[{createdBy:"user1",date:
"2022-01-20", time:"07:31:35"},
createdBy:"user2", date:
"2022-02-20", time: "09:31:37"}]

我正在使用 Angular.js。

请在下面找到可能的解决方案。

const response = [{createdBy:"user1",updatedDttm:'2022-01-20T07:31:35.544Z'},
{createdBy:"user2", updatedDttm: '2022-02-20T09:31:37.544Z'}].map(x => ({
 createdBy: x.createdBy,
 date: new Date(x.updatedDttm).toLocaleDateString(),
 time: new Date(x.updatedDttm).toLocaleTimeString(),
}));

console.log(response);

物有所值,这不是最优雅的解决方案,但如果您的对象中的数据是一致的,您可以尝试:

const a = [
    {
        "createdBy": "user1",
        "updatedDttm": "2022-01-20T07:31:35.544Z"
    },
    {
        "createdBy": "user2",
        "updatedDttm": "2022-02-20T09:31:37.544Z"
    }
];

const response = a.map(x => ({
 createdBy: x.createdBy,
 date: x['updatedDttm'].split('Z')[0].split('T')[0],
 time: x['updatedDttm'].split('Z')[0].split('T')[1].split('.')[0],
}));

console.log(response);

momentjs 可以提供更优雅的解决方案 and/or lodash;