I am able to get the output format that I need, but not the correct time. I need it in GMT (which is +4 hours)
var dt = new Date();
var dt2 = dt.toString('yyyyMMddhhmmss');
Any ideas? The output looks like:
20120403031408
I am able to get the GMT in standard string format by doing:
dt.toUTCString();
but im unable to convert it back to the yyyyMMddhhmmss string
EDIT: I am using the date.js library
date.js
'stoString(format)
doesn't have an option to specify "UTC" when formatting dates. The method itself (at the bottom of the file) never references any ofDate
'sgetUTC...
methods, which would be necessary to support such an option.You may consider using a different library, such as Steven Levithan's
dateFormat
. With it, you can either prefix theformat
withUTC:
, or passtrue
after theformat
:You can also write your own function, as Dominic demonstrated.
The key is to use the
getUTC
functions :https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date