473,394 Members | 1,735 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,394 software developers and data experts.

Compare Dates in Javascript

8
Hi to everybody! I want to compare current date (server date) to the date inputted by the user. It should detect if inputted date is earlier than or equal to the current date.
For example:
Current date is 3/6/3008
Inputted Date by the user is 03/05/2008


Below is my function:

Expand|Select|Wrap|Line Numbers
  1. function checkDateInput(dateString){
  2.                var error="";
  3.     with(currDate){
  4.         currDate = getMonth()+1+"/"+getDate()+"/"+getFullYear();
  5.     }
  6.  
  7.     if(dateString != ""){
  8.         if(currDate >= dateString){
  9.             error = "Publication Date must not earlier than or equal to current date.";
  10.             return error;
  11.         }
  12.     }
  13.     return error;
  14. }
  15.  
Please help me. Thanks in advance and More Power!
Mar 6 '08 #1
5 2072
acoder
16,027 Expert Mod 8TB
Use the Date() object to compare dates, see for example, this link.
Mar 6 '08 #2
rnd me
427 Expert 256MB
Use the Date() object to compare dates, see for example, this link.
that wont validate by the server date, it uses the user's system's date, which can be more easily forged.

you will need to use a very simple xmlHtttpRequest to grab the server date.

would you like that code?
Mar 8 '08 #3
acoder
16,027 Expert Mod 8TB
that wont validate by the server date, it uses the user's system's date, which can be more easily forged.
That's right, but I did mean the JavaScript Date() object being set from the server whilst the page was loading, though an XMLHttp request would perhaps be more accurate (if the user happened to be on the page for more than one day!).
Mar 8 '08 #4
rnd me
427 Expert 256MB
That's right, but I did mean the JavaScript Date() object being set from the server whilst the page was loading, though an XMLHttp request would perhaps be more accurate (if the user happened to be on the page for more than one day!).
how would the Date object be affected by the server date?
i don't see how it has anything to do with page visit length, though i could be mis-understanding something.

if my clock is off, due to hacking or a bad CMOS battery for instance, the Date object will reflect that incorrect date.

this code gets a valid date object using the server's date:

Expand|Select|Wrap|Line Numbers
  1.         function serverDate() {
  2.             X = !window.XMLHttpRequest ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest;
  3.             X.open("GET", window.location, false);
  4.             X.send("");
  5.             return Date(X.getResponseHeader("Date"));
  6.         }
  7.  
Mar 8 '08 #5
acoder
16,027 Expert Mod 8TB
how would the Date object be affected by the server date?
i don't see how it has anything to do with page visit length, though i could be mis-understanding something.

if my clock is off, due to hacking or a bad CMOS battery for instance, the Date object will reflect that incorrect date.
I know that. I meant setting the Date object using server-side code, either using the Date() constructor or the set*** methods. If the user was for some reason still on the same page the next day, the date would now be off by one day (very unlikely, of course, but all the same...)
Mar 9 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

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...
4
by: alexis | last post by:
Hi, In a form I have the curent date <input name="datetoday" type="hidden" value="<? echo date("d/m/Y"); ?>"> and <input type=text name="datebox" size=15> The date format is d/m/Y...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
9
by: Rich | last post by:
Thanks for the Help in my previous post. I've been working on this and it's almost what I want. I want to obtain the user's current age by comparing their date of birth (user inputs) to the...
9
by: Rimuen | last post by:
Have two text form with dates i need to compare before submitting, the second should always be a date later the first one. Anyone have a script for this ?? Thanks.......
5
by: Tom | last post by:
It appears that you can't compare two dates in DotNet. You must use ToString and compare the strings. Is that the only reliable way? Try this: Dim dteOne As Date =...
3
by: Peter | last post by:
ASP.NET 1.1, this is a Intranet application and runs only on IE. I have two webform text boxes which contain dates - starting date and ending date. The dates could be in any format. for...
7
by: joeyej | last post by:
How do I compare today's date with this string (in my inc file) so that I can set an alert if date choice i.e. May 15, 2006 not at least greater than two days from current date? <option...
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
11
by: balakrishnan.dinesh | last post by:
hi frnds, Im having two 20digit numbers, But while comparing those it is giiving wrong ouput in javascript. for example here is my code, my secanrio is , ~ If first 20 digit number is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.