我正在迁移我的数据库中的一些数据并将日期作为字符串"date" : "2020-10-23",
。我想将该日期转换为日期时间对象以将其存储在另一个字段中。我正在使用luxon
let DateTime = luxon.DateTime;
// Function
function timeZoneToUTC(timezone, year, month, day, hours) {
const dateObj = `${year}-${month}-${day} ${hours}:00`;
let date = DateTime.fromFormat(dateObj, 'yyyy-M-d H:mm', {
zone: timezone
});
return date.toUTC().toString();
}
// Example
let record = {preparationTime: "18",
date : "2020-10-23"};
let dateISO = DateTime.fromISO(record.date, {zone: 'Europe/Amsterdam'});
dateISO = dateISO.minus({days: 1});
let year = dateISO.year;
let month = dateISO.month;
let day = dateISO.day;
record.newOrderTime = timeZoneToUTC('Europe/Amsterdam', year, month, day, 22);
console.log(record.newOrderTime);
<script src = "https://cdn.jsdelivr.net/npm/luxon@1.25.0/build/global/luxon.min.js" > </script>
上面的代码返回了一个错误的时间,例如:2020-10-23
将是2020-10-22T20:00:00.000Z
日期正确但时间不正确,应该是 21 而不是 20