One day, my colleague asked about the parameter passing through the new date () function and ran it on Firefox and Google's browser console. Different results were obtained. At first, I thought it was impossible. Later, I found this trap in actual operations.
var date = new Date(‘2014-07-25T23:00:00‘);alert(date);
In Firefox, the returned result is:
Date {Fri Jul 25 2014 23:00:00 GMT + 0800}
Firefox version (output through navigator. useragent): "Mozilla/5.0 (Windows NT 6.1; wow64; RV: 30.0) Gecko/20100101 Firefox/30.0"
Google Chrome returns the following:
Sat Jul 26 2014 07:00:00 GMT + 0800 (Chinese Standard Time)
Google browser version (output through navigator. useragent): "Mozilla/5.0 (Windows NT 6.1; wow64) applewebkit/537.36 (khtml, like gecko) Chrome/36.0.1985.125 Safari/537.36"
In comparison, Google regards the passed string as UTC time, plus 8 hours (Beijing is in GMT), and new date () calls date internally. parse ('2017-07-25t23: 00: 00z') Function
Firefox does not automatically add the local time zone. In Firefox, transfer the UTC format string and add Z (Z indicates Zulu time, which is Greenwich Mean Time ).
Of course, you can add a zero-Time Zone offset "+" to the end of the UTC string, for example, '2017-07-25t23: 00: 00 + 00: 00 '.
The running result of Firefox is as follows:
new Date(‘2014-07-25T23:00:00‘);Date {Fri Jul 25 2014 23:00:00 GMT+0800}new Date(‘2014-07-25T23:00:00Z‘);Date {Sat Jul 26 2014 07:00:00 GMT+0800}new Date(‘2014-07-25T23:00:00+00:00‘);Date {Sat Jul 26 2014 07:00:00 GMT+0800}
The running result of Google Chrome is as follows:
New Date ('2017-07-25t23: 00: 00'); sat Jul 26 2014 07:00:00 GMT + 2014 (China Standard Time) New Date ('2017-07-25t23: 00: 00z'); sat Jul 26 2014 07:00:00 GMT + 0800 (China Standard Time) New Date ('2017-07-25t23: 00: 00 + 00: 00 '); sat Jul 26 2014 07:00:00 GMT + 0800 (Chinese Standard Time)
Because you do not know when the browser adds no partition, we recommend that you write a function to convert the string to the time format:
// Converts a string to a time format. It is applicable to various browsers. The format is 2011-08-03 09: 15: 11 function gettimebytimestr (datestring) {var timearr = datestr. split (""); var d = timearr [0]. split ("-"); var T = timearr [1]. split (":"); new date (d [0], (D [1]-1), d [2], t [0], t [1], T [2]);} // converts time to string format. applicable to various browsers: function gettimestrbytime (time, stringtype) {var y = time. getfullyear (); var M = time. getmonth () + 1; var d = time. getdate (); var H = time. gethours (); var M = Date . Getminutes (); If (stringtype = 1) return y + '-' + (M <10? ('0' + M): m) + '-' + (d <10? ('0' + d): d) + "" + (H <10? ('0' + H): H) + ":" + (M <10? ('0' + M): m); Return y + '/' + (M <10? ('0' + M): m) + '/' + (d <10? ('0' + d): d) + "" + (H <10? ('0' + H): H) + ":" + (M <10? ('0' + M): m );}
In addition, common JSON format data can be extended and converted.
// Normalize the time string in JSON format into a regular string or time string, for example, "\/date (1406217600000 )\/"
Function gettimestrbyjsonstr (jsonstr) {var date = new date (parseint (sonstr. replace ("/date (",""). replace (")/", ""), 10); Return gettimestrbytime (date, 2);} // The time parameter is "July 25" function getdatetimestr (startdate, datestring) {var currenthour = datestring. split ('day') [1]; If (currenthour. indexof ("Hour ")! =-1) currenthour = currenthour. split ('time') [0]; If (currenthour = 0) currenthour = 24; var tempdate = new date (startdate. getfullyear (), startdate. getmonth (), startdate. getdate (); tempdate. settime (tempdate. gettime () + currenthour * 3600000); Return gettimestrbytime (tempdate, 2 );}