if i have some time with UTC now. which coming from backend in json format like:
cache_time: "2019-04-29 06:12:06"
and eg. some user will open page in some another country or city. How i can convert it to this user time with javascript?
if i have some time with UTC now. which coming from backend in json format like:
cache_time: "2019-04-29 06:12:06"
and eg. some user will open page in some another country or city. How i can convert it to this user time with javascript?
Use the Moment.js libary:
date = new Date("2019-04-29 06:12:06Z");
var withoutUTC = moment(date).toString();
var withUTC = moment.utc(date).toString();
console.log(withoutUTC)
console.log(withUTC)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.js"></script>
A string with "2019-04-29 06:12:06" does not indicate the time zone. If you add a 'Z' at the end, the built in Date function understands that the time is in UTC.
const cache_time = "2019-04-29 06:12:06";
const date = new Date ( cache_time + "Z" );
console.log("UTC TIME", date.toISOString())
console.log("LOCAL TIME", date.toLocaleString() );
Use moment.js to convert utc to local time.In your case:
moment().utc("2019-04-29 06:12:06").local().format()
Or you can use spacetime.js, which is based on moment.js and takes into consideration also timezones. const localTimeDate = spacetime().now().format();