By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,627 Members | 1,870 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,627 IT Pros & Developers. It's quick & easy.

date compare within 90days?

P: n/a
Hi,
I have a function where i need to check the date passed in is within
90days of today. Could someone help me get this to work, how do i do
the 90 days compare with today?
<script language='javascript'>
function CheckDate(productDate)
{
var dateVar = new Date(productDate);
//alert(dateVar);
var today = new Date();
//alert(today);
alert( today.getDate()-dateVar.getDate() );
}
</script>

I'm subtracting the date, tried timed, but can't figure out what's it
returning.
Jun 27 '08 #1
Share this Question
Share on Google+
11 Replies


P: n/a
so******@yahoo.com:
I have a function where i need to check the date passed in is within
90days of today. Could someone help me get this to work, how do i do
the 90 days compare with today?
<script language='javascript'>
function CheckDate(productDate)
{
var dateVar = new Date(productDate);
//alert(dateVar);
var today = new Date();
//alert(today);
alert( today.getDate()-dateVar.getDate() );
}
</script>

I'm subtracting the date, tried timed, but can't figure out what's it
returning.
miliseconds ?
divide by 86 400 000 (nr of miliseconds in a day).
Tom
Jun 27 '08 #2

P: n/a
so******@yahoo.com wrote:
I have a function where i need to check the date passed in is within
90days of today.
Parse error. Are you talking about 90 days or about 3 months? Before or
since today?
Could someone help me get this to work, how do i do
the 90 days compare with today?
<script language='javascript'>
<script type="text/javascript">
function CheckDate(productDate)
{
var dateVar = new Date(productDate);
//alert(dateVar);
var today = new Date();
//alert(today);
alert( today.getDate()-dateVar.getDate() );
}
</script>

I'm subtracting the date,
Wrong aproach.
tried timed,
Pardon?
but can't figure out what's it returning.
It *is evaluated to* the number of milliseconds between the dates, with
the provision of its being off by -3'600'000 milliseconds (standard to DST)
or +3'600'000 milliseconds (DST to standard) if DST switching occurs in
between. Therefore, subtraction is the wrong approach here.

var now = new Date(), later = new Date(), before = new Date();

// 90 days from now
later.setDate(now.getDate() + 90);

// 90 days ago
before.setDate(now.getDate() - 90);

You can compare the set date with any given date then.

If you mean months, use getMonth() and setMonth(), respectively.
HTH

PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Jun 27 '08 #3

P: n/a
Tom de Neef wrote:
so******@yahoo.com:
>[...]
alert( today.getDate()-dateVar.getDate() );
}
</script>

I'm subtracting the date, tried timed, but can't figure out what's it
returning.

miliseconds ?
More or less.
divide by 86 400 000 (nr of miliseconds in a day).
Won't work precisely until they have abolished DST and implementations have
taken that into account.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Jun 27 '08 #4

P: n/a
for shure you meant a function in this way ?
<script language='javascript'>
function CheckDate(productDate)
{
var dateVar = Date.parse( productDate );
//alert(dateVar);
var today = new Date();
var Nr_of_Days = ( Date.parse( today ) - dateVar ) / 86400000;
alert( Nr_of_Days );
}
</script>
Jun 27 '08 #5

P: n/a
On Mon, 14 Apr 2008 10:07:40 -0700, roamy wrote:
var Nr_of_Days = ( Date.parse( today ) - dateVar ) / 86400000;
This fails to take daylight savings time changes into account.

getDate() +/- 90 is a much better and bullet proof approach.
Jun 27 '08 #6

P: n/a
In comp.lang.javascript message <48************@PointedEars.de>, Mon, 14
Apr 2008 18:51:24, Thomas 'PointedEars' Lahn <Po*********@web.de>
posted:
>
var now = new Date(), later = new Date(), before = new Date();

// 90 days from now
later.setDate(now.getDate() + 90);
var now = new Date(), later = new Date(+now), before = new Date(+now);

should be somewhat more efficient and also proof against improbable
nocturnal inconsistency. Using new Date() requires that the OS supplies
the date and time, which is not necessarily held in the form that a date
Object holds; but using new Date(+now) merely requires creation of the
Object and copying a Double. The + is not always necessary; omitting it
in this case should do no actual harm, but wastes machine time in IE.

If started VERY late in the day, before could get tomorrow.

The OP should check the exact intent of "within 90 days"; is the 90th
day allowed?

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
Web <URL:http://www.merlyn.demon.co.uk/- w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/- see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jun 27 '08 #7

P: n/a
"Thomas 'PointedEars' Lahn" <Po*********@web.dewrote in message
news:48**************@PointedEars.de...
Tom de Neef wrote:
so******@yahoo.com:
[...]
alert( today.getDate()-dateVar.getDate() );
}
</script>

I'm subtracting the date, tried timed, but can't figure out what's it
returning.
miliseconds ?

More or less.
divide by 86 400 000 (nr of miliseconds in a day).

Won't work precisely until they have abolished DST and implementations
have
taken that into account.
Why? Both times should be in UTC which disregards DST/Summertime (as it's
called in Europe).
Jun 27 '08 #8

P: n/a
Lee
so******@yahoo.com said:
>
Hi,
I have a function where i need to check the date passed in is within
90days of today. Could someone help me get this to work, how do i do
the 90 days compare with today?
<script language='javascript'>
function CheckDate(productDate)
{
var dateVar = new Date(productDate);
//alert(dateVar);
var today = new Date();
//alert(today);
alert( today.getDate()-dateVar.getDate() );
}
</script>

I'm subtracting the date, tried timed, but can't figure out what's it
returning.
And none of the available on-line documentation was any help?
--

Jun 27 '08 #9

P: n/a
In comp.lang.javascript message <Zr****************@nlpi061.nbdc.sbc.com
>, Mon, 14 Apr 2008 17:25:13, Jeremy J Starcher <r3***@yahoo.com>
posted:
>On Mon, 14 Apr 2008 10:07:40 -0700, roamy wrote:
> var Nr_of_Days = ( Date.parse( today ) - dateVar ) / 86400000;

This fails to take daylight savings time changes into account.
Perhaps more importantly, it fails to take the time of today into
account. Nr_of_Days describes an integer.

Since today is a Date Object, +today should be better than
Date.parse(today) which will go via a String - slow, and loses the
milliseconds - and, as there is a subtraction, the + is not needed.

--
(c) John Stockton, nr London UK. ??*@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
Check boilerplate spelling -- error is a public sign of incompetence.
Never fully trust an article from a poster who gives no full real name.
Jun 27 '08 #10

P: n/a
D. Stussy wrote:
"Thomas 'PointedEars' Lahn" [...] wrote [...]
>Tom de Neef wrote:
>>so******@yahoo.com:
[...]
alert( today.getDate()-dateVar.getDate() );
}
</script>

I'm subtracting the date, tried timed, but can't figure out what's it
returning.
miliseconds ?
More or less.
>>divide by 86 400 000 (nr of miliseconds in a day).
Won't work precisely until they have abolished DST and implementations
have
^^^^^^
>taken that into account.

Why? Both times should be in UTC which disregards DST/Summertime (as it's
called in Europe).
But they are not.

Please use ân app that deserves to be called a newsreader for accessing Usenet.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Jun 27 '08 #11

P: n/a
In comp.lang.javascript message <fu**********@snarked.org>, Mon, 14 Apr
2008 23:48:18, D. Stussy <sp**@bde-arc.ampr.orgposted:
>"Thomas 'PointedEars' Lahn" <Po*********@web.dewrote in message
news:48**************@PointedEars.de...
>Tom de Neef wrote:
so******@yahoo.com:
[...]
alert( today.getDate()-dateVar.getDate() );
}
</script>

I'm subtracting the date, tried timed, but can't figure out what's it
returning.

miliseconds ?

More or less.
divide by 86 400 000 (nr of miliseconds in a day).

Won't work precisely until they have abolished DST and implementations
have
>taken that into account.

Why? Both times should be in UTC which disregards DST/Summertime (as it's
called in Europe).
When a difference of 90 days is called for, that in normal circumstances
means calendar days; and those vary in length. Merely dividing the
number of seconds by 86400 is naive.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
Web <URL:http://www.merlyn.demon.co.uk/- w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/- see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jun 27 '08 #12

This discussion thread is closed

Replies have been disabled for this discussion.