在字典列表中解析 int to date
Parse int to date in a list of dictionaries
我的名单:
var list = [{"id": 1, "date": 1649368800000}, {"id": 2, "date": 1649368800000}, {"id": 3, "date": 1649368800000}]
期望的输出:
var list = [{"id": 1, "date": 01.01.2022}, {"id": 2, "date": 1649368800000}, {"id": 3, "date": 01.01.2022}]
如何有效地遍历列表并将“日期”中的 int 值解析为人类可读的日期格式?
一开始我试着只打印日期值..
var list_of_values = [a_dict["date"] for a_dict in list]
但因 解析错误而失败:',' expected
我认为你感到困惑 python with javacript。
short-for 循环是 the python way
自从您标记此 javacript, I'd recommend forEach()
and new Date()
:
解析为日期格式
var list = [{"id": 1, "date": 1649368800000}, {"id": 2, "date": 1649368800000}, {"id": 3, "date": 1649368800000}];
list.forEach(o => o.date = new Date(o.date));
console.log(list);
解析为 dd.mm.yyyy
var list = [{"id": 1, "date": 1649368800000}, {"id": 2, "date": 1649368800000}, {"id": 3, "date": 1649368800000}];
list.forEach(o => {
const d = new Date(o.date);
o.date = `${d.getDate()}.${d.getMonth() + 1}.${d.getFullYear()}`
});
console.log(list);
我的名单:
var list = [{"id": 1, "date": 1649368800000}, {"id": 2, "date": 1649368800000}, {"id": 3, "date": 1649368800000}]
期望的输出:
var list = [{"id": 1, "date": 01.01.2022}, {"id": 2, "date": 1649368800000}, {"id": 3, "date": 01.01.2022}]
如何有效地遍历列表并将“日期”中的 int 值解析为人类可读的日期格式?
一开始我试着只打印日期值..
var list_of_values = [a_dict["date"] for a_dict in list]
但因 解析错误而失败:',' expected
我认为你感到困惑 python with javacript。
short-for 循环是 the python way
自从您标记此 javacript, I'd recommend forEach()
and new Date()
:
解析为日期格式
var list = [{"id": 1, "date": 1649368800000}, {"id": 2, "date": 1649368800000}, {"id": 3, "date": 1649368800000}];
list.forEach(o => o.date = new Date(o.date));
console.log(list);
解析为 dd.mm.yyyy
var list = [{"id": 1, "date": 1649368800000}, {"id": 2, "date": 1649368800000}, {"id": 3, "date": 1649368800000}];
list.forEach(o => {
const d = new Date(o.date);
o.date = `${d.getDate()}.${d.getMonth() + 1}.${d.getFullYear()}`
});
console.log(list);