JS gain attention point in time server

When obtaining server time by js, encountered a small problem, but the impact of the large, so write to remind you that we must be more careful in obtaining validation testing multi-server time.

 

js Use the following method to note two points while getting server time:

1.xhr.open () method, the middle path is best not to write "" or "/" if the write empty, acquisition time there will be problems in the lower part of Google, so it is best to a specific URL.

2. The acquisition time method, you can get the server time is correct, if the server changed area, get here is still time in the server time zone Beijing should be displayed.

    After obtaining the local computer and the server time, conversion time is also the problem on the computer time zone to be displayed native.

    So after the local time zone change, the server will be changed to the local time zone corresponding to the time period of time, we must pay attention! ! ! ! ! ! ! ! ! ! !

You can get to the unity of time is Beijing time zone change, or be changed directly obtained from the background.

 

 

These two methods are also excerpt from another article, the big hope God can forgive ~~~~~~~~~~~~~~~~~~~~

// Get the server time

function getServerDate(){

was XHR = null;

if(window.XMLHttpRequest){

xhr = new window.XMLHttpRequest();

}else{ // ie

xhr = new ActiveObject("Microsoft")

}

xhr.open("GET", "<%=basePath%>user/toupdpwd?id=26",false)//false不可变

xhr.send(null);

var date = xhr.getResponseHeader("Date");

// into Beijing time zone

//return getBeiJingTime(date)

return new Date(date);

}

 

 

// When the time into Beijing Time

function getBeiJingTime(date) {

var d = new Date(date);

var timezone = 8; // target time zone, the East eight districts

var offset_GMT = d.getTimezoneOffset (); // local time and GMT time difference, in minutes

var nowDate = d.getTime (); // local time from the number of milliseconds between (GMT time) in January 1970 midnight on the 1st

var targetDate = new Date(nowDate + offset_GMT * 60 * 1000 + timezone * 60 * 60 * 1000);

console.log ( "East Area 8 now is:" + targetDate);

return targetDate;

}
----------------
Disclaimer: This article is CSDN blogger "yezi_huan 'original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and this link statement.
Original link: https: //blog.csdn.net/yezi_yanyuhuan/article/details/96769160

 

JavaScript gets the current time zone:

new Date (). getTimezoneOffset ()
so acquired with time 0 time zone difference (0:00 zones minus the current location zone in minutes)
China Standard Time is east of the eight districts prevail, time 0:00 zones than earlier 8 hours, so is -480, divided by 60 is the zone where
when the time strings turn into milliseconds, if we do not add time zones, the system will default to the current time zone plus, also from to convert defined time zone
(also the first turn to milliseconds plus the time difference, then into a string of time, so that significantly more trouble)

 

----------------
Disclaimer: This article is CSDN bloggers' password-u "in the original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and link this statement.
Original link: https: //blog.csdn.net/u013992330/article/details/78625855

 

The first: 
        $ .ajax ({ 
            type: "the OPTIONS" , 
            URL: "/" , 
            Complete: function (X) { 
                var DATE = x.getResponseHeader ( "a Date" ); 
                DATE = new new a Date (DATE); 
            } 
        } ); 

results: Mon On Oct 23 is 2017 + 0800 17:26:59 GMT (China standard time) 

The second: 
        function getNowDate () { 
            var XHR = null ;
             IF (window.XMLHttpRequest) { 
                XHR = new new  window.XMLHttpRequest () ;
            } the else{
                xhr = new ActiveObject("Microsoft")
            }

            xhr.open("GET","/",false);
            xhr.send(null);
            var date = xhr.getResponseHeader("Date");
            date = new Date(date);
            return date;
        }
结果:Mon Oct 23 2017 17:26:59 GMT+0800 (中国标准时间)

Original Address: https://www.cnblogs.com/yexiaocangji/p/7717811.html

 

Dependence jQuery

Code:

function getServerDate(){
    return new Date($.ajax({async: false}).getResponseHeader("Date"));
}

 

 

Guess you like

Origin www.cnblogs.com/it-deepinmind/p/12029274.html