Thursday, October 29, 2026
HomeiOS Developmentjavascript - Invalid date in safari

javascript – Invalid date in safari

[ad_1]

Use the beneath format, it could work on all of the browsers

var 12 months = 2016;
var month = 02;           // month varies from 0-11 (Jan-Dec)
var day = 23;

month = month<10?"0"+month:month;        // to make sure YYYY-MM-DD format
day = day<10?"0"+day:day;

dateObj = new Date(12 months+"-"+month+"-"+day);

alert(dateObj); 

//Your output would appear to be this “Wed Mar 23 2016 00:00:00 GMT+0530 (IST)”

//Be aware this could be within the present timezone on this case denoted by IST, to transform to UTC timezone you may embrace

alert(dateObj.toUTCSting);

//Your output now would love this “Tue, 22 Mar 2016 18:30:00 GMT”

Be aware that now the dateObj exhibits the time in GMT format, additionally word that the date and time have been modified correspondingly.

The “toUTCSting” operate retrieves the corresponding time on the Greenwich meridian. This it accomplishes by establishing the time distinction between your present timezone to the Greenwich Meridian timezone.

Within the above case the time earlier than conversion was 00:00 hours and minutes on the twenty third of March within the 12 months 2016. And after conversion from GMT+0530 (IST) hours to GMT (it mainly subtracts 5.30 hours from the given timestamp on this case) the time displays 18.30 hours on the twenty second of March within the 12 months 2016 (precisely 5.30 hours behind the primary time).

Additional to transform any date object to timestamp you should utilize

alert(dateObj.getTime());

//output would look one thing just like this “1458671400000”

This may provide the distinctive timestamp of the time

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments