Update Javascript date time string to match current time zone using MomentJS

I have a Javascript based application (using Nativescript) in which I use the moment to manipulate time a bit. My server returns dtae times to me in the following string format "9/14/2016 4:52:20 PM Whats not in UTC. Depending on where the user is, I would like to change the timestamp of this line to fit their time zone Therefore, I am trying to get a line that will look like "4:52 PM" in EST or "12:52 PM" if it is the Pacific time zone. I am currently trying to do this using MomentJS as follows:

exports.dateTimeStamp = function(value) {
    var utcDate = moment.utc(value);
    var localDate = moment(utcDate).local();
    var formatedDate = moment(localDate).format('LT');
    return formatedDate;
}; 

Unfortunately, this does not work correctly. At the moment, if I try to do this, and the user is at **** EST (GMT-4: 00) ****, and the string value that I pass is equal to "09/14/2016 4: 52: 20 PM “ I am coming back ” 12:52 PM “ Instead of “ 4:52 PM ” Can someone please indicate that I“ Wrong? My thought is to switch to UTC, but it seems that this is necessary from what I read.

EDIT I looked into Moment Timezone, but I don’t think it will work, since my application is global, and it seems that Timezone requires that a hard-coded country / city string be entered into it. In my case, Javascript is used in an Android application. Therefore, I get information about the locale and culture from the device. Maybe I just don’t quite understand how to use it.

+4
source share

Source: https://habr.com/ru/post/1654648/


All Articles