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
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.
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.
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.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With