Issue
I have to show the date and time according to the user's timezone. I have saved the UTC date-time while insertion into DB. I have tried below code but it is working only in Chrome:
let dateTimeConverted = new Date(new Date(jsonObject.calculationDateTime) + '.000Z');
I am able to get the current user's timezone offset using below code:
var x = new Date();
var currentTimeZoneOffsetInHours = x.getTimezoneOffset();
I have tried a few things to convert to user's timezone and show user's timezones' date time, but I wasn't successful might be I am doing something wrong. please help me to figure out this.
Solution
You can use moment.js
let date = new Date().toUTCString();
console.log("UTC Date: " + date); // UTC Date: Thu, 27 Jun 2019 07:50:46 GMT
let localDate = moment(date).local(true).format("YYYY-MM-DD HH:mm:ss");
console.log("Moment Local Date: " + localDate); // Moment Local Date: 2019-06-27 13:20:46
Answered By - schoolcoder
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.