How to convert timestamp to days by javascript
This article introduces the knowledge of "how javascript converts timestamps into days". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
JS converts the year, month, day, minute and second string into a timestamp
Var time = Date.parse (new Date ("2020-08-31 16:00")
A timestamp representing 2020-08-31 16:00 will be output:
1598860800000
Time stamp converted to number of days
The cut-off time minus the current time to calculate the number of days from the end of the distance
Var d = new Date (); var rangeDateNum = (d.getTime ()-time) / (1000 / 3600 / 24)
Ex.: convert 1598860800000 into days
Var time = Date.parse (new Date ("2020-08-31 16:00"); var d = new Date (); var rangeDateNum = (d.getTime ()-time) / (1000 / 3600 / 24); console.log (rangeDateNum)
Output result:
371.9680493634259
Extended knowledge: timestamp to date
/ / parseInt () string to digital / / new Date () into the timestamp without the default current time this.tabledata [I] .createTime = this.formatDate (new Date (parseInt (this.tableData[ I] .createTime)) / / timestamp conversion date format formatDate (now) {var year = now.getFullYear (); / / gets a 4-digit year var month = now.getMonth () + 1 / / get the month in the date, where 0 is January, 11 is December var date = now.getDate (); / / return the number of days in the date month (1 to 31) var hour = now.getHours (); / / the number of hours in the return date (0 to 23) var minute = now.getMinutes (); / / the number of minutes in the return date (0 to 59) var second = now.getSeconds () / / return the number of seconds in a date (0 to 59) return year + "-" + month + "-" + date + "" + hour + ":" + minute + ":" + second;} "how javascript converts timestamps into days". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!