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

Viewed 520

I am using 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)

1 Answers

Your desired format seem to be the one returned by JavaScript Date 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)

Related