Reputation: 843
Okay, say JSON parse string UTC date as below:
2012-11-29 17:00:34 UTC
Now if I want to convert this UTC date to my local time, how can I do this?
How do I format it to something else like yyyy-MM-dd HH:mm:ss z
?
This date.toString('yyyy-MM-dd HH:mm:ss z');
never work out :/
Upvotes: 50
Views: 148912
Reputation: 91
It converts UTC ISO Date to local ISO date format. ("2011-10-05T14:48:00.000Z") using basic native JS method.
convertTimeZoneUTCToLocal(date: string | number): string {
const startTime = new Date(date);
return new Date(startTime.toString().split('GMT')[0] + ' UTC').toISOString();}
You can also use moment.js library to convert UTC time to local with below method.
convertTimeZoneUTCToLocal(date: string | number): string {
return moment.utc(date).local().format('YYYY-MM-DDTHH:mm:ss.SSS');
function convertTimeZoneUTCToLocal(date) {
const startTime = new Date(date);
return new Date(startTime.toString().split('GMT')[0] + ' UTC').toISOString();
}
function loadDate(date){
document.getElementById("outputDate").innerHTML = convertTimeZoneUTCToLocal(date);
}
<html>
<head>
<script>
</script>
</head>
<body onload="loadDate('2024-08-23T07:33:51.242Z');">
<div> Input Date : 2024-08-23T07:33:51.242Z </div>
<div> Output Date: <span id="outputDate"></span></div>
</body>
</html>
}
Upvotes: 0
Reputation: 41
Here is another option that outputs mm/dd/yy using toLocaleString()
:
const date = new Date('2012-11-29 17:00:34 UTC');
date.toLocaleString();
//output 11/29/2012
Upvotes: 4
Reputation: 141
// d = "2021-09-23T15:51:48.31"
console.log(new Date(d + "z").toLocaleDateString()); // gives 9/23/2021
console.log(new Date(d + "z").toLocaleString()); // gives 9/23/2021, 10:51:48 AM
console.log(new Date(d + "z").toLocaleTimeString()); // gives 10:51:48 AM
Upvotes: 1
Reputation: 383
This works for both Chrome and Firefox.
Not tested on other browsers.
const convertToLocalTime = (dateTime, notStanderdFormat = true) => {
if (dateTime !== null && dateTime !== undefined) {
if (notStanderdFormat) {
// works for 2021-02-21 04:01:19
// convert to 2021-02-21T04:01:19.000000Z format before convert to local time
const splited = dateTime.split(" ");
let convertedDateTime = `${splited[0]}T${splited[1]}.000000Z`;
const date = new Date(convertedDateTime);
return date.toString();
} else {
// works for 2021-02-20T17:52:45.000000Z or 1613639329186
const date = new Date(dateTime);
return date.toString();
}
} else {
return "Unknown";
}
};
// TEST
console.log(convertToLocalTime('2012-11-29 17:00:34 UTC'));
Upvotes: 0
Reputation: 17408
You could take a look at date-and-time api for easily date manipulation.
let now = date.format(new Date(), 'YYYY-MM-DD HH:mm:ss', true);
console.log(now);
<script src="https://cdn.jsdelivr.net/npm/date-and-time/date-and-time.min.js"></script>
Upvotes: 1
Reputation: 3970
The solutions above are right but might crash in FireFox and Safari! and that's what webility.js is trying to solve. Check the toUTC
function, it works on most of the main browers and it returns the time in ISO format
Upvotes: 1
Reputation: 3896
var offset = new Date().getTimezoneOffset();
offset
will be the interval in minutes from Local time to UTC. To get Local time from a UTC date, you would then subtract the minutes from your date.
utc_date.setMinutes(utc_date.getMinutes() - offset);
Upvotes: 42
Reputation: 1
/*
* convert server time to local time
* simbu
*/
function convertTime(serverdate) {
var date = new Date(serverdate);
// convert to utc time
var toutc = date.toUTCString();
//convert to local time
var locdat = new Date(toutc + " UTC");
return locdat;
}
Upvotes: -8
Reputation: 29
To format your date try the following function:
var d = new Date();
var fromatted = d.toLocaleFormat("%d.%m.%Y %H:%M (%a)");
But the downside of this is, that it's a non-standard function, which is not working in Chrome, but working in FF (afaik).
Chris
Upvotes: 2
Reputation: 3498
This should work
var date = new Date('2012-11-29 17:00:34 UTC');
date.toString()
Upvotes: 0
Reputation: 30565
Try:
var date = new Date('2012-11-29 17:00:34 UTC');
date.toString();
Upvotes: 56