So I’m trying to convert a UTC time to the clients local time in short format. I am using the following code:
var date = new Date(call.Timestamp);
date.toLocaleString()
And I get the output like this on the screen:
Thu Sep 13 2012 14:33:15 GMT+1000 (AUS Eastern Standard Time)
What I would like to do is once I have converted it to the date object to display the short format:
dd/mm/yyyy hh:mm:ss or mm/dd/yyyy hh:mm:ss depending on what timezone they are in.
Is there a standard way I can do this in javascript? Does anyone know of a library package that can do this for me?
I have also tried:
date.toLocaleDateString() + ' ' + date.toLocaleTimeString()
but the date string still outputs: “Thursday, September 13, 2012” rather than
Working with Dates in JavaScript has long been a pain in the neck. A library that I’ve found to be the best so far is Sugar.js.
Sugar has extended the
Dateobject with some really powerful and expressive methods for date creation and manipulation. I think thatDate.create()alone will take care of what you need. It’s a much better version of a Date constructor so it will still return a Date object. It takes two option parameters. One for locale and one for utc.Here you can see the method details.