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

How to compare 2 dates?

Hi i want to compare to dates and alert the user if he enters a date earlier than the current date. The user enters the date by using the javascript date time picker format is (yyyy-mm-dd).
These are the codes:

Expand|Select|Wrap|Line Numbers
  1. var dt = document.form1.txtDateFrm.value;
  2.     var enddt=document.form1.txtDateTo.value;
  3.     alert(dt)
  4.  
  5.     var thedate = new Date();
  6.     var thisyear = thedate.getYear( );
  7.     var thismonth = thedate.getMonth( ) + 1;
  8.     var thisday = thedate.getDate( );
  9.     var currentDate = thisyear+"-"+thismonth+"-"+thisday;
  10.     alert(currentDate)
  11.  
  12.     if (new Date(dt) < new Date(currentDate))
  13.     {
  14.         alert("Date From cannot be earlier than the current date.")
  15.     return false;
  16.     }
I have tried using
Expand|Select|Wrap|Line Numbers
  1. var dt = new Date(document.form1.txtDateFrm.value);
but when i alert the var dt, it returns NaN.

The if statement is not working as well..

How should i solve this problem?
Thank you..
Jul 30 '07 #1
3 2933
shweta123
692 Expert 512MB
Hi,

You can try this
Link

Hi i want to compare to dates and alert the user if he enters a date earlier than the current date. The user enters the date by using the javascript date time picker format is (yyyy-mm-dd).
These are the codes:

Expand|Select|Wrap|Line Numbers
  1. var dt = document.form1.txtDateFrm.value;
  2.     var enddt=document.form1.txtDateTo.value;
  3.     alert(dt)
  4.  
  5.     var thedate = new Date();
  6.     var thisyear = thedate.getYear( );
  7.     var thismonth = thedate.getMonth( ) + 1;
  8.     var thisday = thedate.getDate( );
  9.     var currentDate = thisyear+"-"+thismonth+"-"+thisday;
  10.     alert(currentDate)
  11.  
  12.     if (new Date(dt) < new Date(currentDate))
  13.     {
  14.         alert("Date From cannot be earlier than the current date.")
  15.     return false;
  16.     }
I have tried using
Expand|Select|Wrap|Line Numbers
  1. var dt = new Date(document.form1.txtDateFrm.value);
but when i alert the var dt, it returns NaN.

The if statement is not working as well..

How should i solve this problem?
Thank you..
Jul 30 '07 #2
vee10
141 100+
Hi ,

The following code can solve ur problem
Expand|Select|Wrap|Line Numbers
  1. var dt = document.form1.txtDateFrm.value;
  2.     var enddt=Date.parse(document.form1.txtDateTo.value);
  3.     alert(dt);
  4.       var l_dt = new Date();
  5.      var today = Date.UTC( l_dt.getFullYear(), l_dt.getMonth()+1, l_dt.getDate(),0,0,0);
  6.      alert(today);
  7.     thisyear=dt.substring(0,dt.indexOf("-"));
  8.     dt=dt.substring(dt.indexOf("-")+1);  
  9.     thismonth = dt.substring(0,dt.indexOf("-"));
  10.     dt=dt.substring(dt.indexOf("-")+1);  
  11.     var start = Date.UTC(thisyear,thismonth,dt,0,0,0);
  12.     alert(start);
  13.     if(start > today)
  14.     {
  15.         alert("Date From cannot be earlier than the current date.")
  16.     return false;
  17.     }
  18.  
if any wrong in my code please inform me










Hi i want to compare to dates and alert the user if he enters a date earlier than the current date. The user enters the date by using the javascript date time picker format is (yyyy-mm-dd).
These are the codes:

Expand|Select|Wrap|Line Numbers
  1. var dt = document.form1.txtDateFrm.value;
  2.     var enddt=document.form1.txtDateTo.value;
  3.     alert(dt)
  4.  
  5.     var thedate = new Date();
  6.     var thisyear = thedate.getYear( );
  7.     var thismonth = thedate.getMonth( ) + 1;
  8.     var thisday = thedate.getDate( );
  9.     var currentDate = thisyear+"-"+thismonth+"-"+thisday;
  10.     alert(currentDate)
  11.  
  12.     if (new Date(dt) < new Date(currentDate))
  13.     {
  14.         alert("Date From cannot be earlier than the current date.")
  15.     return false;
  16.     }
I have tried using
Expand|Select|Wrap|Line Numbers
  1. var dt = new Date(document.form1.txtDateFrm.value);
but when i alert the var dt, it returns NaN.

The if statement is not working as well..

How should i solve this problem?
Thank you..
Jul 31 '07 #3
Hi,
Thanks for your help.
It is working extremely well.

Thank you :)
Aug 3 '07 #4

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

Similar topics

4
by: Gleep | last post by:
Hey Guys, I've got a table called Outcomes. With 3 columns and 15 rows 1st col 2nd col 3rdcol outcome date price There are 15 rows...
3
by: jrc4728 | last post by:
I have a MySQL table with the date stored in three fields as string values like this. (sorry, its imported data) str_yy str_dd str_mm ------------------------ 05 01 04 05 ...
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.......
1
by: godsella | last post by:
First i have two stored procedures, i have passed the values of each one into two different arraylists of dates. how can i compare the two arraylists of dates? Thanks in advance
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 =...
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...
6
by: cd123 | last post by:
Hi, I used Date object of Javascrips to compare two dates like one is the from date and the other is to date. var obj1 = new Date(from date); var obj2 = new Date(to date); then i used...
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
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
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.