demand

Computes the difference between the current time and 8 o ‘clock this morning. If the current time is earlier than 8 o ‘clock, return “00:00:00”, otherwise return “hh:mm:ss”.

Analysis of the

When I first saw this question, my first reaction was to get the current time first, and then get “eight o ‘clock today “, subtract the two to get the time difference

To solve

Originally is only a method, but then a flash of inspiration, the result of the method two, because of a flash of inspiration, let me understand, I am a brother….. Please look down

  • Method 1: calculate the millisecond difference, and then figure out how many hours + minutes + seconds the difference contains

SetHours (8,0,0,0).sethours (8,0,0,0).sethours Specify the time (new Date()), and the hour field is the hour field for this time

The experimental code

let millisec = new Date().setHours(8.0.1.0);// Return the number of milliseconds 1630800001000
console.log(new Date(millisec).toLocaleString())// result is '2021/9/5 8:00:01 am '
Copy the code

Well, since it was settled at 8:00, that’s half the problem solved (I think so)

The implementation of

function getDifferenceTime(){
    let nowTime = Date.now();// Get the number of milliseconds corresponding to the current time
    let eightTime = new Date().setHours(8.0.0.0);// Get the number of milliseconds corresponding to eight
    let differenceTime = nowTime-eightTime;
    if(differenceTime <= 0) {return "00:00:00"
    }else{
        // Find the current time difference in seconds, the result is n seconds more than m milliseconds
        let seconds = Math.floor(differenceTime / 1000);
        // Find the number of minutes corresponding to the current time difference, and find the remaining seconds
        let minutes = Math.floor(seconds / 60);
        let remainSec = seconds % 60;// Number of seconds left
        // Find the number of hours corresponding to the current time difference, and find the remaining minutes
        let hours = Math.floor(minutes / 60);
        let remainMin = minutes % 60;
        / / the return value
        return `${hours.toString().padStart(2 , "0")}:${remainMin.toString().padStart(2."0")}:${remainSec.toString().padStart(2."0")}`}}console.log(getDifferenceTime());/ / 08:07:55
Copy the code

OK!!! You’re done

  • Method 2 is……. Suddenly a thought struck me:

New Date() generates a Date that is 0 minutes 0 seconds 0 milliseconds from January 1, 1970, based on the number of milliseconds passed in. Can we directly pass in the time difference, directly read the hours, minutes and seconds of the time difference generated, and then join together, and then everything will be ok?

The implementation of

function getDifferenceTime(){
    let nowTime = Date.now();// Get the number of milliseconds corresponding to the current time
    let eightTime = new Date().setHours(8.0.0.0);// Get the number of milliseconds corresponding to eight
    let differenceTime = nowTime-eightTime;
    if(differenceTime <= 0) {return "00:00:00"
    }else{
        let diffDate = new Date(differenceTime);
        let hours = diffDate.getHours();
        let minutes = diffDate.getMinutes();
        let seconds = diffDate.getSeconds();
        return `${hours.toString().padStart(2."0")}:${minutes.toString().padStart(2."0")}:${seconds.toString().padStart(2."0")}`}}Copy the code

Confident thought, steady, I really cowhide, results print: Console. log(getDifferenceTime())//16:27:23 I was frozen on the spot, it was my afternoon time, emmmmm, this is the f * * k, the thinking is right, I fat four, think, think, think… Crack! So time to the next morning, after lunch break, so suddenly jumped up, repeated in the mind, passed through the number of milliseconds, come to January 1, 1970 in Greenwich after how long, ou roar!!!!!!!!!!!!! Greenwich highlights!!!!!!! When we create a new Date() object, the time we get is automatically converted to the time of east 8. New Date(1000) Thu Jan 01 1970 08:00:01 GMT+0800 (Chinese Standard time) Since we are using standard time in east 8, we simply subtract eight hours from the difference in incoming time, or add the corresponding hour if we are in Western time:

Once again the implementation

function getDifferenceTime(){
    let nowTime = Date.now();// Get the number of milliseconds corresponding to the current time
    let eightTime = new Date().setHours(8.0.0.0);// Get the number of milliseconds corresponding to eight
    let differenceTime = nowTime-eightTime;
    if(differenceTime <= 0) {return "00:00:00"
    }else{
        let diffDate = new Date(differenceTime - 8*60*60*1000);
        let hours = diffDate.getHours();
        let minutes = diffDate.getMinutes();
        let seconds = diffDate.getSeconds();
        return `${hours.toString().padStart(2."0")}:${minutes.toString().padStart(2."0")}:${seconds.toString().padStart(2."0")}`}}console.log(getDifferenceTime())/ / 08:43:29 perfect!!!!!!!!!!!!!!!!!!!
Copy the code

I wrote this article this afternoon, so you don’t have to worry too much about the timing. This is my first article in the nuggets hope you can nuggets friends have so a little help, thank you!!