Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 127059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:24:59+00:00 2026-05-11T05:24:59+00:00

For example, given two dates in input boxes: <input id="first" value="1/1/2000"/> <input id="second" value="1/1/2001"/>

  • 0

For example, given two dates in input boxes:

<input id="first" value="1/1/2000"/> <input id="second" value="1/1/2001"/>  <script>   alert(datediff("day", first, second)); // what goes here? </script> 

How do I get the number of days between two dates in JavaScript?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T05:25:00+00:00Added an answer on May 11, 2026 at 5:25 am

    Here is a quick and dirty implementation of datediff, as a proof of concept to solve the problem as presented in the question. It relies on the fact that you can get the elapsed milliseconds between two dates by subtracting them, which coerces them into their primitive number value (milliseconds since the start of 1970).

    /**  * Take the difference between the dates and divide by milliseconds per day.  * Round to nearest whole number to deal with DST.  */ function datediff(first, second) {             return Math.round((second - first) / (1000 * 60 * 60 * 24)); }  /**  * new Date('dateString') is browser-dependent and discouraged, so we'll write  * a simple parse function for U.S. date format (which does no error checking)  */ function parseDate(str) {     var mdy = str.split('/');     return new Date(mdy[2], mdy[0] - 1, mdy[1]); }  alert(datediff(parseDate(first.value), parseDate(second.value)));
    <input id='first' value='1/1/2000'/> <input id='second' value='1/1/2001'/>

    You should be aware that the "normal" Date APIs (without "UTC" in the name) operate in the local timezone of the user’s browser, so in general you could run into issues if your user is in a timezone that you don’t expect, and your code will have to deal with Daylight Saving Time transitions. You should carefully read the documentation for the Date object and its methods, and for anything more complicated, strongly consider using a library that offers more safe and powerful APIs for date manipulation.

    • Numbers and Dates — MDN JavaScript Guide
    • Date — MDN JavaScript reference

    Also, for illustration purposes, the snippet uses named access on the window object for brevity, but in production you should use standardized APIs like getElementById, or more likely, some UI framework.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example: given two dates below, finish is always greater than or equal to start
This is easier to explain with an example. Given these two classes: public class
For example, given the list ['one', 'two', 'one'] , the algorithm should return True
Given the following HTML example... <div id='div1'>div one</div> <div id='div2'>div two</div> ...I found that
I am storing two dates start/end date incremented by weeks, so for example I
Given a table with the value of individual items in inventory for particular dates,
I have an array with timestamps. If this timestamps are between two given dates
Possible Duplicate: Calculate number of days between two given dates Is there a way
I'm working on a python script that needs to run between two given times.
Could anyone give me an example about how to do fuzzy matching of two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.