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

testing if date is in past

P: n/a
How would I write some Javascript to test whether a date is in the past
or not? I need to only accept either the current date or future dates.

*** Sent via Developersdex http://www.developersdex.com ***
Oct 30 '08 #1
Share this Question
Share on Google+
16 Replies


P: n/a
Mike P wrote:
How would I write some Javascript to test whether a date is in the past
or not? I need to only accept either the current date or future dates.

Well
var now = new Date();
gives you the current date. Then construct a second date e.g.
var d1 = new Date(2009,0,1);
and compare them e.g.
if (d1 >= now)

--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 30 '08 #2

P: n/a
Martin Honnen wrote on 30 okt 2008 in comp.lang.javascript:
Mike P wrote:
>How would I write some Javascript to test whether a date is in the past
or not? I need to only accept either the current date or future dates.


Well
var now = new Date();
gives you the current date. Then construct a second date e.g.
var d1 = new Date(2009,0,1);
and compare them e.g.
if (d1 >= now)
if now were 2009/jan/1 11:33
then (d1 >= now) would be false.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 30 '08 #3

P: n/a
In comp.lang.javascript message <49***********************@newsspool1.ar
cor-online.net>, Thu, 30 Oct 2008 18:44:43, Martin Honnen
<ma*******@yahoo.deposted:
>Mike P wrote:
var now = new Date();
gives you the current date. Then construct a second date e.g.
var d1 = new Date(2009,0,1);
and compare them e.g.
if (d1 >= now)
Better IMHO to write d1 as new Date("2009/01/01") in general because
the second argument of new Date(y,m,d) does not match the month-
numbering used in the Outside World.

One needs to think about whether the present day should be considered as
in the past or not. One may want to use now.setHours(0,0,0,0) or
now.setHours(24,0,0,0) --- remembering that in the Azores there is one
day each year which does not start at 00:00:00 and another that has
00:00:00 twice - I think.

--
(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.
Oct 30 '08 #4

P: n/a
In comp.lang.javascript message <Xn********************@194.109.133.242>
, Thu, 30 Oct 2008 21:06:20, Evertjan. <ex**************@interxnl.net>
posted:
>
if now were 2009/jan/1 11:33
then (d1 >= now) would be false.
I find it quicker to put a unary + before each date Object than to try
to remember how relational operators work between date Objects; YMMV.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)
Oct 31 '08 #5

P: n/a
Dr J R Stockton wrote on 31 okt 2008 in comp.lang.javascript:
In comp.lang.javascript message <Xn********************@194.109.133.242>
, Thu, 30 Oct 2008 21:06:20, Evertjan. <ex**************@interxnl.net>
posted:
>>
if now were 2009/jan/1 11:33
then (d1 >= now) would be false.

I find it quicker to put a unary + before each date Object than to try
to remember how relational operators work between date Objects; YMMV.
True, but that would not help in the above case, John.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 31 '08 #6

P: n/a
In comp.lang.javascript message <Xn********************@194.109.133.242>
, Fri, 31 Oct 2008 21:56:51, Evertjan. <ex**************@interxnl.net>
posted:
>Dr J R Stockton wrote on 31 okt 2008 in comp.lang.javascript:
>In comp.lang.javascript message <Xn********************@194.109.133.242>
, Thu, 30 Oct 2008 21:06:20, Evertjan. <ex**************@interxnl.net>
posted:
>>>
if now were 2009/jan/1 11:33
then (d1 >= now) would be false.

I find it quicker to put a unary + before each date Object than to try
to remember how relational operators work between date Objects; YMMV.

True, but that would not help in the above case, John.
No : my other article refers to that problem but does not say + is
necessary.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Nov 1 '08 #7

P: n/a
Dr J R Stockton wrote:
Martin Honnen posted:
>Mike P wrote:
var now = new Date();
gives you the current date. Then construct a second date e.g.
var d1 = new Date(2009,0,1);
and compare them e.g.
if (d1 >= now)

Better IMHO to write d1 as new Date("2009/01/01") in general because
the second argument of new Date(y,m,d) does not match the month-
numbering used in the Outside World.
It would be rather unwise to do that as when the first argument of the Date
constructor is a string value, it is specified in ECMAScript Ed. 3 Final,
section 15.9.3.2, that the value should be parsed "in exactly the same
manner as for the parse method (section 15.9.4.2); let V be the time value
for this date.".

However it is specified there that "The string may be interpreted as a local
time, a UTC time, or a time in some other time zone, depending on the
contents of the string."; in other words, it is _not_ specified there.
Therefore, using "YYYY/DD/MM" may or may not work in an implementation, and
for that matter that implementation would still be compliant with ECMAScript
Ed. 3.

In contrast, the numeric multi-argument variant is well-specified in section
15.9.3.1, and well-supported.

A wrapper method or developer's algorithm (subtract 1) can provide for the
syntactic sugar; however, for a developer it should not be unusual to
encounter zero-based indexes, so the Outside World argument must be
questioned here.
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
Nov 2 '08 #8

P: n/a
In comp.lang.javascript message <49**************@PointedEars.de>, Sun,
2 Nov 2008 12:20:20, Thomas 'PointedEars' Lahn <Po*********@web.de>
posted:
>Dr J R Stockton wrote:
>Martin Honnen posted:
>>Mike P wrote:
var now = new Date();
gives you the current date. Then construct a second date e.g.
var d1 = new Date(2009,0,1);
and compare them e.g.
if (d1 >= now)

Better IMHO to write d1 as new Date("2009/01/01") in general because
the second argument of new Date(y,m,d) does not match the month-
numbering used in the Outside World.

It would be rather unwise to do that as when the first argument of the Date
constructor is a string value, it is specified in ECMAScript Ed. 3 Final,
section 15.9.3.2, that the value should be parsed "in exactly the same
manner as for the parse method (section 15.9.4.2); let V be the time value
for this date.".

However it is specified there that "The string may be interpreted as a local
time, a UTC time, or a time in some other time zone, depending on the
contents of the string."; in other words, it is _not_ specified there.
Therefore, using "YYYY/DD/MM" may or may not work in an implementation, and
for that matter that implementation would still be compliant with ECMAScript
Ed. 3.

In contrast, the numeric multi-argument variant is well-specified in section
15.9.3.1, and well-supported.

A wrapper method or developer's algorithm (subtract 1) can provide for the
syntactic sugar; however, for a developer it should not be unusual to
encounter zero-based indexes, so the Outside World argument must be
questioned here.
You are a legalistic twit. I've tested versions of five major browsers
myself, and all accept that form. I've asked for counterexamples, and
none have ever been provided. By your reasoning, no string form would
be usable.

Granted, there are two known traps with new Date(String).

One cannot safely use DD/MM/YYYY or MM/DD/YYYY, since both forms are in
use in the Real World, and a browser might choose to prefer either.
YYYY/MM/DD is safe; one has to be really stupid to choose YYYY/DD/MM or
YYYYDDMM - AFAIK, that has only ever been done at the Federal level.

And one cannot safely use alphabetic offset indicators other than GMT &
UTC, since they are not otherwise standardised. NASA do not show signs
of knowing that, though they have been told.

Neither trap applies in this case.

As for your silly "zero-based" argument - the Gregorian Month in the
real world is always numerically represented by 1 to 12, as per ISO
8601. Therefore, it is unreasonable to expect people editing code
always to remember to use numbers 0 to 11, whereas if they are editing a
YYYY/MM/DD string there will be no problem.

You should not be arguing against supporting an important and useful ISO
standard.

As far as I can see, de.c.l.j also find you to be offensively socially
abnormal (which is a relief, in a way). You should try to grow up and
act as a normal human being. Psycho-medical services can do wonders
these days, they say.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)
Nov 2 '08 #9

P: n/a
On 2008-11-03 00:38, Dr J R Stockton wrote:
[..] whereas if they are editing a YYYY/MM/DD string there will be
no problem.

You should not be arguing against supporting an important and useful
ISO standard.
ISO standard is "YYYY-MM-DD".

In software, date formats that contain slashes always make me run for
cover. And yes, I have actually seen people use the form YYYY/DD/MM.
- Conrad
Nov 3 '08 #10

P: n/a
In comp.lang.javascript message <UdCdnXHl4ZFN1JPUnZ2dnUVZ_q7inZ2d@supern
ews.com>, Mon, 3 Nov 2008 01:46:07, Conrad Lender <cr******@yahoo.com>
posted:
>On 2008-11-03 00:38, Dr J R Stockton wrote:
>[..] whereas if they are editing a YYYY/MM/DD string there will be
no problem.

You should not be arguing against supporting an important and useful
ISO standard.

ISO standard is "YYYY-MM-DD".

In software, date formats that contain slashes always make me run for
cover. And yes, I have actually seen people use the form YYYY/DD/MM.
One supports the Standard by coming as near as is practical to using it.
Had I meant following it, which implies full compliance, I would have
used that word. But, as no known (by me) browser will accept new
Date("2008-11-03") , it is new Date("2008/11/03") which should be
used to set that date, LCT, in code.

When programming, one can use
new Date(FORM.control.value.replace(/-/g. "/"))
to permit any mix of - & / and .replace(/(\d\d)(\d\d)$/, "/$1/$2")
to permit YYYYMMDD.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
Nov 3 '08 #11

P: n/a
Conrad Lender wrote:
On 2008-11-03 00:38, Dr J R Stockton wrote:
>>
You should not be arguing against supporting an important and useful
ISO standard.

ISO standard is "YYYY-MM-DD".
I suspect the good doctor is aware of that, as is anyone who's read
his Date and Time Miscellany.

And ISO 8601:2004(E) specifies a number of formats, not just the one
you cite.

--
Michael Wojcik
Micro Focus
Rhetoric & Writing, Michigan State University
Nov 4 '08 #12

P: n/a
On 2008-11-04 17:20, Michael Wojcik wrote:
>>You should not be arguing against supporting an important and useful
ISO standard.

ISO standard is "YYYY-MM-DD".

I suspect the good doctor is aware of that, as is anyone who's read
his Date and Time Miscellany.
I must admit, I hadn't heard of the Doctor's treatise on Time yet ;-)
I was merely pointing out that the form "YYYY/MM/DD" is not part of the
standard he mentioned.

That browsers won't accept the "YYYY-MM-DD" notation is unfortunate; but
I've never seen the need to initialize a Date with a string. It's very
common in other programming languages to use [0,11] as the month index,
so I didn't find it confusing at all. Maybe the "outside world" has
different needs.
And ISO 8601:2004(E) specifies a number of formats, not just the one
you cite.
True, but the slash is strictly used for intervals and durations. The
(optional) separator for days, months, and years is a dash.

3.4.4 Characters used as separators
In representations the following characters are used as separators:

[-] (hyphen): to separate the time elements "year" and "month", "year"
and "week", "year" and "day", "month" and "day", and "week" and
"day";
[:] (colon): to separate the time elements "hour" and "minute", and
"minute" and "second";
[/] (solidus): to separate components in the representation of time
intervals and recurring time intervals.

I don't want to be overly argumentative about this. If the "YYYY/MM/DD"
format works for you, that's fine by me. Just don't use "it's The
Standard" as an argument when the standard contradicts the statement.
- Conrad
Nov 4 '08 #13

P: n/a
Dr J R Stockton wrote:
Conrad Lender posted:
>On 2008-11-03 00:38, Dr J R Stockton wrote:
>>[..] whereas if they are editing a YYYY/MM/DD string there will be
no problem.

You should not be arguing against supporting an important and useful
ISO standard.
ISO standard is "YYYY-MM-DD".

In software, date formats that contain slashes always make me run for
cover. And yes, I have actually seen people use the form YYYY/DD/MM.

One supports the Standard by coming as near as is practical to using it.
Had I meant following it, which implies full compliance, I would have
used that word. But, as no known (by me) browser will accept new
Date("2008-11-03") , it is new Date("2008/11/03") which should be
used to set that date, LCT, in code.
You must be kidding. You can *meet* the standard already by using the
numeric variant, yet you recommend that one should run the risk of getting
a wrong time value by assuming that unspecified behavior was interoperable
in the attempt to come *as close as possible* to another that is entirely
irrelevant here.

There's a word for such an argumentation, and I guess you know it.
When programming, one can use
new Date(FORM.control.value.replace(/-/g. "/"))
to permit any mix of - & / and .replace(/(\d\d)(\d\d)$/, "/$1/$2")
to permit YYYYMMDD.
It's a good possibility, but insufficient to back up your argument. In
fact, accessibility guidelines call for user interfaces to provide separate
controls for date input.
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
Nov 4 '08 #14

P: n/a
Conrad Lender wrote:
On 2008-11-04 17:20, Michael Wojcik wrote:
>>>You should not be arguing against supporting an important and useful
ISO standard.
ISO standard is "YYYY-MM-DD".
I suspect the good doctor is aware of that, as is anyone who's read
his Date and Time Miscellany.

I must admit, I hadn't heard of the Doctor's treatise on Time yet ;-)
It's worth a look, though.
I was merely pointing out that the form "YYYY/MM/DD" is not part of the
standard he mentioned.

That browsers won't accept the "YYYY-MM-DD" notation is unfortunate; but
I've never seen the need to initialize a Date with a string. It's very
common in other programming languages to use [0,11] as the month index,
so I didn't find it confusing at all. Maybe the "outside world" has
different needs. [...]
s/browsers/known language implementations so far/, otherwise full ACK.
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Nov 4 '08 #15

P: n/a
Conrad Lender wrote:
On 2008-11-04 17:20, Michael Wojcik wrote:
>>>You should not be arguing against supporting an important and useful
ISO standard.
ISO standard is "YYYY-MM-DD".
I suspect the good doctor is aware of that, as is anyone who's read
his Date and Time Miscellany.

I must admit, I hadn't heard of the Doctor's treatise on Time yet ;-)
I was merely pointing out that the form "YYYY/MM/DD" is not part of the
standard he mentioned.
True. Your statement was a bit ambiguous - I wasn't sure if you
intended it to be read as "YYYY-MM-DD is the only format endorsed by
the ISO standard", which of course it isn't. (The Basic format for the
complete representation of a calendar date omits the hyphens, for
example.)

Of course, my response was a bit ambiguous too.
That browsers won't accept the "YYYY-MM-DD" notation is unfortunate; but
I've never seen the need to initialize a Date with a string. It's very
common in other programming languages to use [0,11] as the month index,
so I didn't find it confusing at all. Maybe the "outside world" has
different needs.
Agreed. Though I do worry that the zero-based month index could trip
up a maintainer. On the other hand, how often does the typical program
initialize a Date?

--
Michael Wojcik
Micro Focus
Rhetoric & Writing, Michigan State University
Nov 6 '08 #16

P: n/a
In comp.lang.javascript message <ge********@news2.newsguy.com>, Wed, 5
Nov 2008 22:13:18, Michael Wojcik <mw*****@newsguy.composted:
>Agreed. Though I do worry that the zero-based month index could trip
up a maintainer. On the other hand, how often does the typical program
initialize a Date?
>Rhetoric & Writing, Michigan State University
But what proportion of programs are typical?

I often look at pages by NASA, its associates, and their rivals. It is
quite common for them to have a countdown. with the critical date/time
embedded in the (over-long) code.

Sometimes they use new Date(Y, M, D, h, m, s) or Date.UTC(Y, M, D,
h, m, s) ; sometimes they use strings, which may have the month in full,
in short, or numeric. And with strings they often give an offset
indicator such as EST or PDT (in spite of their *predictable* events
being fundamentally UTC-based) (obviously, only the Washington area, on
either coast, is important) -- which Opera will not deign to recognise.
Of course, their testing will be done only with MSIE, and only in the
more important parts of the USA!

However, they can easily find worse code flaws to make - perhaps they
are paid per line of code, and per bug subsequently fixed.

--
(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.
Nov 6 '08 #17

This discussion thread is closed

Replies have been disabled for this discussion.