Two hours ago
When you evaluate the time period control on the Interface today, you must obtain the current time and the time two hours before the current time. How can we implement it in JavaScript?
The new date () in javascript can get the current time.
The first thought was to use
VaR date = new date (). getdate ();
To obtain the number of hours of the current time, and then subtract two hours to calculate the desired result, but a lot of judgment is required. For example, after two hours, the threshold value of 0 is exceeded, yesterday, however, yesterday can be the last month, and the number of days in the last month must be recalculated. This is troublesome.
A good way is to skip these steps and check the following code:
VaR now = new date ();
VaR start = new date (0, 0, 0, 0, 0, 0, 0); // note that there are seven digits, and the last digit is millisecond.
// The difference between the current time and the minimum time, in milliseconds. The difference between the time before two hours and the minimum time is naturally 3600*1000*2 less than the current time.
VaR millis = now-start-3600*1000*2;
VaR twohoursago = new date (0, 0, 0, 0, 0, 0, millis );
UTC time of the current time
VaR time = new date ();
VaR utcsecond = time. gettimezoneoffset (); // you can find the difference between the current time and the current UTC time.
VaR interval = Time-new date (0, 0, 0, 0, 0, 0, 0) + utcsecond * 60000;
VaR utctime = new date (0, 0, 0, 0, 0, 0, interval );