How to convert timestamp to number of days in javascript
Conversion method: First obtain the timestamp of the current time and the timestamp of the specified date; then subtract the two obtained timestamps to obtain the time difference; finally calculate the "time difference / (1000*3600 *24)" expression value is enough.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
JS converts the year, month, day, hour, minute and second string into a timestamp
var time = Date.parse(new Date("2020-08-31 16:00"));
will output a timestamp representing 2020-08-31 16:00:
1598860800000
Convert timestamp to number of days
Subtract the current time from the deadline to calculate the number of days until the deadline
var d = new Date(); var rangeDateNum = (d.getTime()-time) / (1000*3600*24);
Example: 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 results:
371.9680493634259
Extended knowledge: converting timestamp to date
//parseInt()字符串转数字 //new Date()放入时间戳,没有放默认当前时间 this.tableData[i].createTime = this.formatDate(new Date(parseInt(this.tableData[i].createTime))) // 时间戳转换日期格式 formatDate(now) { var year = now.getFullYear(); //取得4位数的年份 var month = now.getMonth() + 1; //取得日期中的月份,其中0表示1月,11表示12月 var date = now.getDate(); //返回日期月份中的天数(1到31) var hour = now.getHours(); //返回日期中的小时数(0到23) var minute = now.getMinutes(); //返回日期中的分钟数(0到59) var second = now.getSeconds(); //返回日期中的秒数(0到59) return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second; }
[Recommended learning: javascript advanced tutorial 】
The above is the detailed content of How to convert timestamp to number of days in javascript. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

Golang time conversion: How to convert timestamp to string In Golang, time operation is one of the very common operations. Sometimes we need to convert the timestamp into a string for easy display or storage. This article will introduce how to use Golang to convert timestamps to strings and provide specific code examples. 1. Conversion of timestamps and strings In Golang, timestamps are usually expressed in the form of integer numbers, which represent the number of seconds from January 1, 1970 to the current time. The string is

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

In Go, you can use regular expressions to match timestamps: compile a regular expression string, such as the one used to match ISO8601 timestamps: ^\d{4}-\d{2}-\d{2}T \d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-][0-9]{2}:[0-9]{2})$ . Use the regexp.MatchString function to check if a string matches a regular expression.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Golang Programming Tool: Best Practices for Timestamp Obtaining Introduction: In software development, timestamp is a very common concept. It is a numeric value that identifies the occurrence of a specific event, usually representing the number of milliseconds or nanoseconds since some reference point in time. In Golang, processing timestamps is very simple and efficient. This article will introduce the best practices for obtaining timestamps in Golang and provide specific code examples. Text: Get the current timestamp In Golang, getting the current timestamp is very simple. we can

Timestamp in PHP is an integer form representing time, usually the number of seconds that have passed since the first year of Unix (January 1, 1970 00:00:00 GMT). In programming, we often need to convert timestamps into other forms of integers. Here we will introduce how to convert PHP timestamps into integers, as well as specific code examples. In PHP, we can use the strtotime() function to convert the time string to a timestamp and then use date
