/    Sign up×
Community /Pin to ProfileBookmark

Help with comparing dates

Hi,
I have a date textfield that takes a date in the form dd/mm/yyyy. I obtain the current date using a Javascript function.

I want to compare these two dates using javascript. i.e. the number of days between these 2 dates.

It is not simply a case of putting the dates into two variables and subtracting them. They must need parsing first but dont know how to do this. Does anyone know how to do this?
thanks a lot

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@Khalid_AliJul 11.2003 — I think the link below will help you do that

http://68.145.35.86/skills/javascripts/DateRangeSelector.html
Copy linkTweet thisAlerts:
@damon2003authorJul 11.2003 — thanks, will have a look at this to see if I can adapt it. Should just be able to hide the first menu and use it for current date.

Why is there a 'month' option in the section box - it actually gives a value back.

What is the diff measured in. The second one is days, is the measured in seconds?

thanks
Copy linkTweet thisAlerts:
@Mr_JJul 11.2003 — Will this do?



<SCRIPT LANGUAGE="JavaScript">

<!--

function calc_date2() {

from=document.f3.date_from.value

to=document.f3.date_to.value

var fromArray = from.split('/');

fromdate = new Date( fromArray[2], fromArray[1]-1, fromArray[0]);

var toArray = to.split('/');

todate = new Date( toArray[2], toArray[1]-1, toArray[0]);

document.f3.daynums.value=(todate-fromdate)/86400000

}

//-->

</SCRIPT>

<P>Calculate the days from start date to end date.

<center>

<form name="f3">

<P><table border=1>

<tr><td>Start Date</td><td>End Date</td><td>Number of days</td></tr>

<tr><td><input type="text" name="date_from" size="10" value="25/09/2002"></td>

<td><input type="text" name="date_to" size="10" value="26/09/2002"></td>

<td align="center"><input type="text" name="daynums" size="4" maxlength="4" value=""></td></tr>

<tr><td colspan=3 align=center>

<input type="button" value="calculate" onclick="calc_date2()">

</td></tr></table>

</form>

</center>
Copy linkTweet thisAlerts:
@CharlesJul 11.2003 — [font=georgia]Since the Date constructor understands this date format we can shorten that quite a bit. And since the constructor also understands other formats as well, the user doesn't have to be limited to just one format.[/font]

[font=monospace]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"

"http://www.w3.org/TR/html4/strict.dtd">

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<meta name="Content-Script-Type" content="text/javascript">

<title>Example</title>

<style type="text/css">

<!--

label {display:block; margin:1em 0em}

input {margin-right:1em; width:5em}

-->

</style>

<script type="text/javascript">

<!--

Date.ONE_SECOND = 1000;

Date.ONE_MINUTE = Date.ONE_SECOND * 60;

Date.ONE_HOUR = Date.ONE_MINUTE *
60;

Date.ONE_DAY = Date.ONE_HOUR * 24;

// -->

</script>

<form onsubmit="this.diff.value = (new Date(this.stop.value) - new Date(this.start.value)) / Date.ONE_DAY; return false">

<div>

<label><input type="text" name="start">Start</label>

<label><input type="text" name="stop">Stop</label>

<hr>

<label><input type="text" name="diff" onfocus="this.blur()">Difference</label>

<button type="submit">Calculate</button>

</div>

</form>[/font]
Copy linkTweet thisAlerts:
@damon2003authorJul 12.2003 — Khalid, do you have the date-control.js and range-gui.js files?

thanks a lot
Copy linkTweet thisAlerts:
@CharlesJul 12.2003 — [font=georgia]You can find them at http://68.145.35.86/skills/javascripts/scripts/date-control.js and http://68.145.35.86/skills/javascripts/scripts/date-range-gui.js. Note however that he's simply duplicating a lot of stuff that's already built in to the JavaScript Date class and is using at least one depricated feature. You would do better to study how the Date class works. See http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/date.html.[/font]
Copy linkTweet thisAlerts:
@damon2003authorJul 12.2003 — Thanks a lot Charles,

I will study the documentation as do need to understand more
×

Success!

Help @damon2003 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...