Home > other >  How to format to timestamp with Luxon
How to format to timestamp with Luxon

Time:12-02

I'm switching from moment to Luxon. Currently, using moment I can can convert a Date() to timestamp format with this:

       ....moment()
      .subtract(2, 'hours')
      .utc()
      .format('YYYY-MM-DD HH:mm:ss [GMT]');

with the result looking like : 2022-12-01 17:40:16 GMT

switching over to luxon and looking at the docs, I don't see any option to quite get me to that desired result. I've tried this:

const tryThis = date.minus({ hours: 2 }).toISO();
const getGMTIntheDate = DateTime.fromISO(tryThis);
const finalResult = getGMTIntheDate.toJSDate().toString();

which gives me this result: Thu Dec 01 2022 09:49:44 GMT-0800 (Pacific Standard Time)

is there a custom formatting option or am I missing something in the docs?

CodePudding user response:

I think you are looking for datetimetoformat. The formatting details are available here.

DateTime.now().toFormat('yyyy-MM-dd HH:mm:ss [GMT]')

CodePudding user response:

something like this is what I was looking for

date.toUTC().toFormat('yyyy-MM-dd HH:mm:ss ZZZZ')

  • Related