luxon 从 YYYY-MM-DD 获取带有 GMT 的本地日期字符串

luxon get local date string with GMT from YYYY-MM-DD

我正在使用 luxon for my app. I have simple date string like this 2022-02-10 and I want to convert them into Thu Feb 10 2022 00:00:00 GMT+0200 (Eastern European Standard Time). But don't know how to do it in luxon

const { DateTime } = require("luxon");

const date = "2022-02-10";

const dateTimeWithGMT = DateTime.fromISO(date);

console.log(dateTimeWithGMT);

// expected output

// Thu Feb 10 2022 00:00:00 GMT+0200 (Eastern European Standard Time)

您想要的格式似乎是 JavaScript 日期 toString, so you can simply convert luxon's DateTime object to native JavaScript Date using toJSDate()

返回的格式

const DateTime = luxon.DateTime;

const date = "2022-02-10";
const dateTimeWithGMT = DateTime.fromISO(date);
console.log(dateTimeWithGMT.toJSDate().toString());
<script src="https://cdn.jsdelivr.net/npm/luxon@2.3.0/build/global/luxon.js"></script>

Formatting section of the docs properly describes how to format Luxon DateTime objects. Luxon supports ISO 8601 formats (see toISO()), human readable formats (see toLocaleString) and custom formats (see toFormat)