Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localize UTC time/date

i have a timestamp which comes from server (utc). I now want to transform this timestamp to my local time.

Transform UTC:

2016-08-11 12:19:14

To local time:

2016-08-11 14:19:14

This is what i have used:

localizeTime = function (timeToLocalize = "2016-08-11 12:19:14") {
    return moment(timeToLocalize).locale(deviceLocale = "de").format('LLL');
};

I am working with react-native and moment.js

like image 353
BigPun86 Avatar asked Aug 11 '16 15:08

BigPun86


People also ask

What is UTC date/time format?

Times are expressed in UTC (Coordinated Universal Time), with a special UTC designator ("Z"). Times are expressed in local time, together with a time zone offset in hours and minutes. A time zone offset of "+hh:mm" indicates that the date/time uses a local time zone which is "hh" hours and "mm" minutes ahead of UTC.

How do I localize a date?

For instance, in the US, they specify the month first, followed by the day, and then the year. Each component is also separated with a slash, which gives us mm/dd/yyyy , for example: 09/01/2021 , which would be September 1, 2021.

Is Java Util date UTC?

util. Date is the number of milliseconds since the Unix epoch, which occurred at midnight January 1st 1970, UTC. The same epoch could also be described in other time zones, but the traditional description is in terms of UTC. As it's a number of milliseconds since a fixed epoch, the value within java.


1 Answers

If the input time is UTC, and you don't have anything in the string to indicate such, then you need to parse it with moment.utc instead of just with moment. You can then convert it to local time with the local function.

moment.utc("2016-08-11 12:19:14").local().format("YYYY-MM-DD HH:mm:ss")

You don't need to involve locales (like de) unless you really want a locale-specific string format. Locale has to do with language and culture, not with time zones. "local" != "locale"

like image 179
Matt Johnson-Pint Avatar answered Oct 10 '22 04:10

Matt Johnson-Pint