473,382 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Date Comparison



Hi there !!!

I would like to know how to compare dates in javascript.
var sdate = new Date(theform.SubmissionDate.value);
var odate = new Date(theform.StartDate.value);
var todaysdate = new Date();

if(sdate < todaysdate)
{
alert("Submissionn Date should not be less than current date");
return false;
}

if(sdate < odate)
{
alert("Submissionn Date should not be less than opening date");
return false;
}
My above comparison does'nt work. can u suggest some way???

Mar 13 '06 #1
3 10201
Tiya wrote:
I would like to know how to compare dates in javascript.
To compare Date objects, compare their .getTime() values.
var sdate = new Date(theform.SubmissionDate.value);
var odate = new Date(theform.StartDate.value);


This assumes the input fields contain date strings that can be automatically
parsed by the browser. Bad assumption.
Instead, either require the user to enter dates in a specific format or try
to parse a variety of common formats to retrieve the Date object.
Using the lib here: http://www.javascripttoolbox.com/lib/date/
you can easily parse a date string using Date.parseString()

So your code could look something like this:

var sdate = Date.parseString(theform.SubmissionDate.value);
if (sDate.isBefore(new Date()) {
...
}

Hope that helps.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Mar 13 '06 #2
Matt Kruse wrote:
Tiya wrote:
I would like to know how to compare dates in javascript.


To compare Date objects, compare their .getTime() values.


That is not necessary for relational operators. Implicit type conversion
to Number would call their .valueOf() method which returns the same
value .getTime() returns. See ECMAScript Edition 3, section 11.8.
PointedEars
Mar 13 '06 #3
JRS: In article <11*********************@i40g2000cwc.googlegroups. com>,
dated Mon, 13 Mar 2006 02:56:31 remote, seen in
news:comp.lang.javascript, Tiya <Sw*********@gmail.com> posted :
I would like to know how to compare dates in javascript.

var sdate = new Date(theform.SubmissionDate.value);
var odate = new Date(theform.StartDate.value);
var todaysdate = new Date();

if(sdate < todaysdate)
{
alert("Submissionn Date should not be less than current date");
return false;
}

if(sdate < odate)
{
alert("Submissionn Date should not be less than opening date");
return false;
}
My above comparison does'nt work. can u suggest some way???

Slovenly ---^

The comparison code works correctly, and compares dates as desired.
Use, say,
alert(sdate + "\n" + odate + "\n" + todaysdate)
to see what your date strings have been interpreted as. Remember that
strings ##/##/## are liable to be taken as Merkin FFF, and perhaps in
the wrong centade.

Since real users may input incorrectly, put the dates in the alerts.

If sdate is a pure Date, and represents today, the message for the
comparison with todaysdate is questionable.

Read the newsgroup FAQ; see below.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Mar 13 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Donnal Walter | last post by:
I was very surprised to discover that >>> import datetime >>> x = datetime.date(2004, 9, 14) >>> y = datetime.datetime(2004, 9, 14, 6, 43, 15) >>> print x == y True How can these two...
2
by: Scott Knapp | last post by:
Good Day - I have a form which sets the current date, as follows: <script type="text/javascript"> xx=new Date() dd=xx.getDate() mm=xx.getMonth()+1 yy=xx.getYear() mmddyy=mm+"/"+dd+"/"+yy...
1
by: sylvian stone | last post by:
Hi, I've used standard date functions in the past, but need to create something a little different, as I am working on an investment calculator. What I need to do is validate two dates, and...
3
by: Karl Gibbon | last post by:
Hi There, I currently have a database in Access 2002 with several forms. I would like to restrict access to one form in perticular until November 1st every year. My current method (attempted...
6
by: MarkAurit | last post by:
Im having difficulty coming up with a good algorithm to express the following comparison: "if <a given date> falls between the (current date - 5 days) and the (current date)" Obviously....
4
by: blini | last post by:
Helo.... How I can convert string "26/03/2006 15:51" for a date? I need to convert and to compare if "09/06/2006 14:20" is lesser or equal that the current date. Everything in Javascript.
7
by: mr.nimz | last post by:
hello, this is antenio. recently i've come to a problem. i got a way through it, somehow, still it left me in a curious state, so i'm posting it here, if i can get an answer from some techy, ...
3
by: noone | last post by:
Hi, I am designing an application which displays news topics until midnight on the DisplayUntil date and then they should drop out. Unfortunately, they seem to be dropping out at mid-day. I'm...
4
by: anagai | last post by:
I just want to check if a date entered in a textbox is equal to the current system date. I set the date object from the input field like this: dt1=new Date('10/01/2007'); the current system...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.