Step 1: How to convert from time to timestamp

1. The method one

var titme = Date.parse(new Date());
Copy the code

2. The method 2

var titme = new Date().getTime();
Copy the code

Step 2: calculate the time difference between the two times

Var titme = new Date().gettime (); Var titm = new Date("2019/10/2 00:00:00").gettime (); var settime = titme - titm; var lefttime = Math.floor(settime / 1000); // day var d = Math. Floor (lefttime/(24 * 60 * 60)); Var h = math.floor ((lefttime/(60 * 60)) % 24); //分 var m = math.floor ((lefttime / 60) % 60); // second var s = math. floor(lefttime % 60); console.log(d, h,go( m ),go( s));Copy the code

This step is the zero complement function

And the above response to the need to complement the call function can be

Function go(x) {if (x < 10) {return "0" + x; } else { return x; }}Copy the code

The third step calculates the countdown of two times

function go(x) { if (x < 10) { return "0" + x; } else { return x; Var titme = new Date("2022/10/2 00:00:00").gettime ();} function titme = new Date("2022/10/2 00:00:00").getTime(); Var titm = new Date().gettime (); var settime = titme - titm; console.log(settime); var lefttime = Math.floor(settime / 1000); var d = Math.floor(lefttime / 60 / 60 / 24); var h = Math.floor((lefttime / (60 * 60)) % 24); var m = Math.floor((lefttime / 60) % 60); var s = Math.floor(lefttime % 60); console.log(d, h, this.go(m), this.go(s)); } // countdown setInterval(() => {gos(); }, 1000);Copy the code

Step 4: year month day hour minute second writing method

Var titme = new Date(); / var/year year = titme. GetFullYear (); Var Month=titme.getMonth()+1; / var/day data = titme. GetDate (); . / / var Hours = titme getHours (); / var/points Minutes = titme. GetMinutes (); Var / / SEC getSeconds = titme. GetSeconds (); console.log(year,Month,data,Hours,Minutes,getSeconds)Copy the code