Home >Web Front-end >JS Tutorial >javaScript calculates the difference in days between two dates (sample code)_javascript skills

javaScript calculates the difference in days between two dates (sample code)_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 17:06:461109browse

1: Calculate the number of days between two dates

For example:
str1 = "2002-01-20"
str2 = "2002-10-11"
How to use JavaScript to calculate the number of days between str1 and str2?

Copy code The code is as follows:










2: Calculate the date after a certain number of days

In JavaScript, calculate the date after today's date. It is far less convenient than in .Net. A function can solve the problem. This problem troubled me for a while, and finally the problem was solved through the introduction of a netizen. Post it and share it.

Copy code The code is as follows:



The 100 above represents the date 100 days later, you can modify it. Date.getTime() in JS can only support dates after 1970.01.01; and the month is 0 - 11, which is a bit different, so avoid it. Of course you can also calculate dates after a specific date.
Copy code The code is as follows:


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn