Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert UTC Date to datetime string Javascript

I have a UTC date string, "2011-10-30T18:30:00Z", and I want to convert it to "2011-10-30 18:30". Can anyone give me some advice how to do this? I would appreciate for your time and consideration.

Thanks

like image 631
Sinal Avatar asked Oct 10 '11 03:10

Sinal


People also ask

How to convert UTC time into local date time using JavaScript?

Given an UTC date and the task is to convert UTC date time into local date-time using JavaScript toLocaleString() function. Syntax: var theDate = new Date(Date.parse('06/14/2020 4:41:48 PM UTC')) theDate.toLocaleString() JavaScript code:

How to convert a string into a date in JavaScript?

How to Convert a String into a Date in JavaScript. The best format for string parsing is the date ISO format with the JavaScript Date object constructor. But strings are sometimes parsed as UTC and sometimes as local time, which is based on browser vendor and version. It is recommended is to store dates as UTC and make computations as UTC.

Is it possible to convert creationtime to UTC?

we are able to get CreationTime into a String variable but unable to convert to UTC. Any idea to convert this? Show activity on this post. In Javascript you can use this method to convert a date from a Date () object, but not from a IST string.

Should I parse a date as UTC or local time?

But strings are sometimes parsed as UTC and sometimes as local time, which is based on browser vendor and version. It is recommended is to store dates as UTC and make computations as UTC. To parse a date as UTC, you should append a Z: The "Z" is crucial to ensure that the string is interpreted as UTC, not as local time.


2 Answers

The easiest solution is to try the following:

var date = new Date("2011-10-30T18:30:00Z");

This will convert it into a normal Javascript date, at which point you can use whatever data operators you like.

like image 200
NT3RP Avatar answered Oct 24 '22 17:10

NT3RP


str=str.replace(/T|\:\d\dZ/g,' ')
like image 20
kennebec Avatar answered Oct 24 '22 17:10

kennebec