Seems the most foolproof way to start with a UTC date is to create a new Date object and use the setUTC… methods to set it to the date/time you want.
Then the various toLocale…String methods will provide localized output.
Example:
// This would come from the server.
// Also, this whole block could probably be made into an mktime function.
// All very bare here for quick grasping.
d = new Date();
d.setUTCFullYear(2004);
d.setUTCMonth(1);
d.setUTCDate(29);
d.setUTCHours(2);
d.setUTCMinutes(45);
d.setUTCSeconds(26);
console.log(d); // -> Sat Feb 28 2004 23:45:26 GMT-0300 (BRT)
console.log(d.toLocaleString()); // -> Sat Feb 28 23:45:26 2004
console.log(d.toLocaleDateString()); // -> 02/28/2004
console.log(d.toLocaleTimeString()); // -> 23:45:26
Some references:
- toLocaleString
- toLocaleDateString
- toLocaleTimeString
- getTimezoneOffset
Seems the most foolproof way to start with a UTC date is to create a new Date object and use the setUTC… methods to set it to the date/time you want.
Then the various toLocale…String methods will provide localized output.
Example:
// This would come from the server.
// Also, this whole block could probably be made into an mktime function.
// All very bare here for quick grasping.
d = new Date();
d.setUTCFullYear(2004);
d.setUTCMonth(1);
d.setUTCDate(29);
d.setUTCHours(2);
d.setUTCMinutes(45);
d.setUTCSeconds(26);
console.log(d); // -> Sat Feb 28 2004 23:45:26 GMT-0300 (BRT)
console.log(d.toLocaleString()); // -> Sat Feb 28 23:45:26 2004
console.log(d.toLocaleDateString()); // -> 02/28/2004
console.log(d.toLocaleTimeString()); // -> 23:45:26
Some references:
- toLocaleString
- toLocaleDateString
- toLocaleTimeString
- getTimezoneOffset
You can do it with moment.js (deprecated in 2021)
It's best to parse your date string from UTC as follows (create an ISO-8601 compatible string on the server to get consistent results across all browsers):
var m = moment("2013-02-08T09:30:26Z");
Now just use m in your application, moment.js defaults to the local timezone for display operations. There are many ways to format the date and time values or extract portions of it.
You can even format a moment object in the users locale like this:
m.format('LLL') // Returns "February 8 2013 8:30 AM" on en-us
To transform a moment.js object into a different timezone (i.e. neither the local one nor UTC), you'll need the moment.js timezone extension. That page has also some examples, it's pretty simple to use.
Note: Moment JS recommends more modern alternatives, so it is probably not a good choice for new projects.
You might want to check out 10 ways to format time and date using javascript.
You can do something like this, for example, for each of the dates:
var example_date = '2017-09-14T22:11:05.5303556';
function formatDate(date) {
var d = new Date(date),
month = d.getMonth(),
date = d.getDate(),
year = d.getFullYear(),
hours = ('0' + d.getHours()).slice(-2),
minutes = ('0' + d.getMinutes()).slice(-2),
seconds = ('0' + d.getSeconds()).slice(-2);
month++;
return (month + '/' + date +'/' + year + ' ' + hours + ':' + minutes + ':' + seconds);
}
console.log(formatDate(example_date));
You could also make use of .toLocaleTimeString() and .toLocaleDateString() and combine the two.
If you're not averse to using a 3rd party library, I'd recommend taking a gander at MomentJS. It's very good at allowing you to easily format date/time, e.g...
for (var i in json){
json[i].dateCreated = moment(json[i].dateCreated).format('MM/DD/YYYY hh/mm/ss');
}
...where json is the object returned, would produce:
[{"id":98,"dateCreated":"09/14/2017 10/11/05"},{"id":99,"dateCreated":"09/14/2017 10/11/05"}]
I think the new Date(date).toLocaleDateString() would get the job done
Example:
$.ajax({
url: 'http://mywebsite/api/Response',
dataType: 'json',
success: function(json) {
const data = json.map((obj) => (
Object.assign(
{},
obj,
{ dateCreated: new Date(obj.dateCreated).toLocaleDateString() }
)
));
myTable = $('#my-table').columns({
data: data,
schema: [
{ "header": "ID", "key": "id" },
{ "header": "Date", "key": "dateCreated" }
],
});
}
});
I hope that helps ^__^

