Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to calculate the date difference of javascript

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to find the date difference in javascript". In the daily operation, I believe many people have doubts about how to find the date difference in javascript. The editor consulted all kinds of data and sorted out a simple and easy-to-use operation method. I hope it will be helpful to answer the doubt of "how to find the date difference in javascript". Next, please follow the editor to study!

Javascrip to calculate the date difference method: 1, create a HTML sample file; 2, convert two dates into a timest 3, and then divided by the number of milliseconds of each day to get the number of days apart.

This article operating environment: windows7 system, javascript1.8.5 version, DELL G3 computer

How does javascript calculate the date difference?

The method of calculating date difference by javascript

The details are as follows:

Function daytonow (year, month, date) {/ / the idea is to convert two dates into timestamps, that is, milliseconds, and then divide by the milliseconds of each day to get how many days apart / / the month in JS starts at zero, so month should subtract one month--; / / past days var tdate = new Date (year, month, date). GetTime (); / / today var tnow = new Date (). GetTime () Var longdate = Math.ceil ((tnow-tdate) / (1000 * 60 * 60 * 24); return longdate;} alert (daytonow (2009, 4, 5))

The difference between the days of the two dates:

/ / the difference between the days of the two date strings. Before-after, sDate1-sDate2function DateDiff (sDate1, sDate2) {/ / sDate1 and sDate2 are "2002-12-18" format var aDate, oDate1, oDate2, iDays; aDate = sDate1.split ("-"); oDate1 = new Date (aDate [0], aDate [1]-1, aDate [2]); aDate = sDate2.split ("-"); oDate2 = new Date (aDate [0], aDate [1]-1, aDate [2]) IDays = parseInt (Math.abs (oDate1-oDate2) / 1000 / 60 / 60 / 24); if ((oDate1-oDate2) < 0) {return-iDays;} return iDays;} / / the difference between the days of the two date strings, before-after, sDate1-sDate2function DateDiff2 (sDate1, sDate2) {/ / sDate1 and sDate2 are the format var oDate1, oDate2, iDays; oDate1 = new Date (sDate1); oDate2 = new Date (sDate2) Var iDays = parseInt (Math.abs (oDate1-oDate2) / 1000 / 60 / 60 / 24); if ((oDate1-oDate2) < 0) {return-iDays;} return iDays;} at this point, the study on "how to find the date difference in javascript" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report