简体   繁体   中英

Moment time is returning UTC time instead of local time

Currently I am working on a project to create excel report when user clicks on Download Report Button from Application. Everything is working fine except the file name which is getting downloaded.

The problem is when we check from US (CST Time with Daylight Savings), suppose if user downloads the report at Nov 26, 9:00 pm, the file name is getting as Nov 27, 2020. We are using moment in node.js application for getting the time.

const date = moment().format('LL');

Can somebody knows where I am missing. If I am correct, the moment() function will return local time instead of UTC time. But seems to be getting UTC time instead of local time.

Do we need to use moment().local() instead?

Any help would be really appreciated.

Thanks in Advance.

I suppose you should pass a user timezone offset in URL:

// -300 - (CST with daylight savings)
https://somesite.com/api/report?offset=-300 

And on a backend to change an offset with moment.js like this:

// old versions of a moment.js
const date = moment().zone(offset).format('LL')
// new versions of a moment.js
const date = moment().utcOffset(offset).format('LL')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM