473,785 Members | 3,142 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compare Dates in Javascript

8 New Member
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 2085
acoder
16,027 Recognized Expert Moderator MVP
Use the Date() object to compare dates, see for example, this link.
Mar 6 '08 #2
rnd me
427 Recognized Expert Contributor
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 Recognized Expert Moderator MVP
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 Recognized Expert Contributor
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 Recognized Expert Moderator MVP
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
10215
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 document.write(mmddyy)
4
21532
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 (day/month/year) in both case Now my problem is that i want to know if 'datebox' is valid, that means after 'datetoday'.
4
5391
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 calculate days, months, and years. This is not for a college course. It's for my own personal genealogy website. I'm stumped about the code. I'm working on it but not making much progress. Is there any free code available anywhere? I know it...
9
3943
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 current date. I know how to get the Current Date but I'm not finding how to calculate the Current Date minus the User's Birthday.
9
5644
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
6989
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 = FileDateTime(Application.ExecutablePath) Dim dteTwo As Date = FileDateTime(Application.ExecutablePath) SaveSetting("Test", "Dates", "DateThree", FileDateTime(Application.ExecutablePath)) Dim dteThree As Date = CDate(GetSetting("Test", "Dates",
3
2593
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 example: Starting date: 02/15/2006 Ending date: March 2006 I have to make sure the starting date is before the ending date
7
23068
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 value="May 15, 2006, (Monday), 10am">May 15, 2006, (Monday), 10am Thanks, Joe
12
29472
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 follows: function doDateCheckNow(source, args) { var oDate = document.getElementById(source.controltovalidate); // dd/mm/yyyy
11
17816
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 greater number than second 20 digit number ,then it should return.
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10151
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.