Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check Existing Date object is in UTC - Javascript

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.

like image 691
Ashish Jain Avatar asked Nov 30 '25 18:11

Ashish Jain


1 Answers

JavaScript Date object has the method getTimezoneOffset(). You could use that. Or use the getUTC* methods:

 var d = new Date();
 var utcyear = d.getUTCFullYear();
like image 76
Bart Friederichs Avatar answered Dec 03 '25 11:12

Bart Friederichs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!