admin管理员组文章数量:1301575
I've a timestamp from my database at the gmt zone. How can I display the time based on the user timezone? Currently I`m getting the time like this
return luxon.DateTime.fromSQL(created_at).setZone('local');
It returns the time in gtm.
I've a timestamp from my database at the gmt zone. How can I display the time based on the user timezone? Currently I`m getting the time like this
return luxon.DateTime.fromSQL(created_at).setZone('local');
It returns the time in gtm.
Share edited Oct 17, 2018 at 18:56 Asif 3502 silver badges10 bronze badges asked Oct 17, 2018 at 18:44 MarkusMarkus 2,0604 gold badges29 silver badges64 bronze badges2 Answers
Reset to default 10A cleaner way to handle it is to just tell the parser that the time is expressed in GMT:
DateTime.fromSQL(current_time, {zone: "utc"}).toLocal()
To explain that a bit more, what's happening in your original is that the time string is meant to be interpreted as a GMT time, but Luxon doesn't know that. So it interprets the string as a local time, which is off from the time you meant by the offset. But if Luxon knows "this is expressed in GMT" then it will get the right time in the first place.
Problem solved by using this method:
let offset = - (new Date().getTimezoneOffset() / 60);
return luxon.DateTime.fromSQL(created_at).plus({hours: offset}).toLocaleString(luxon.DateTime.TIME_SIMPLE)
本文标签: javascript luxon gmt time to localeStack Overflow
版权声明:本文标题:javascript luxon gmt time to locale - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741678651a2392036.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论