I have used a Javascript function for converting date to UTC date as follows:
Date.prototype.convertToUTC = function () {
var month = this.getMonth();
var day = this.getDate();
var year = this.getFullYear();
return new Date(Date.UTC(year, month, day));
}
Now the problem arises when the date on which this function is applied is already in UTC. Since I don’t know whether user will call this method on UTC/Local date, I want to ensure conversion happens only if it is not in UTC. Please help.
All dates have a UTC time value at their heart. Date objects created in a host are given a read–only timezone offset based on the system settings, and values read using getDate, getHours, etc. are based on that offset.
If you want UTC milliseconds since the epoch, just use the getTime() method. Alternatively, there are the UTC methods, getUTCDay, getUTCHours, etc. to build your own formatted string.
Finally, there is toISOString which should return an ISO formatted date string for UTC, but support may be lacking in not so old browsers.
Some examples
To create a local date object for 2012-11-06T15:45:01Z:
To get an ISO date string from that (or any) date object:
To get a UTC time value in milliseconds (ms since 1970-01-01T00:00:00Z):
To turn that time value back into a local date object: