IT Community - Software Programming, Web Development and Technical Support

Comparing two Dates

This is a discussion on Comparing two Dates within the ASP and ASP.NET Programming forums, part of the Web Development category; Hi, I want to find whether the given date was gone or will come after by checking the current system ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > ASP and ASP.NET Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 03-25-2008, 12:07 AM
Kirubhananth Kirubhananth is offline
D-Web Programmer
 
Join Date: Feb 2008
Location: My native is Madurai but now in Chennai
Posts: 61
Kirubhananth is on a distinguished road
Send a message via AIM to Kirubhananth Send a message via Skype™ to Kirubhananth
Question Comparing two Dates

Hi,

I want to find whether the given date was gone or will come after by checking the current system date.

I have a labelbox and is entering a date in it. I am comparing it with the system date as follows.


DateTime dt = DateTime.Parse(TextBox1.Text);

if (DateTime.Compare(dt, DateTime.Now) < 0)
Label1.Text = "The date was gone.";
else if (DateTime.Compare(dt, DateTime.Now) == 0)
Label1.Text = "It is the current date.";
else
Label1.Text = "The date is yet to come";


The output is only

"The date was gone" or "The date is yet to come"

and not "It is the current date."

Eventhough it is current date the ouput is

"The date was gone".


So, how can we compare it more effeciently ?

I want to know whether the date was gone or is it the current date or it is the future date ?

Last edited by Kirubhananth : 03-25-2008 at 12:11 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-25-2008, 01:31 AM
poornima poornima is offline
D-Web Sr.Programmer
 
Join Date: Dec 2007
Posts: 189
poornima is on a distinguished road
Smile Re: Comparing two Dates

Hi Kirubananth,
U r using DateTime.Now it will give the Date in the following format like this,
3/25/2008 12:48 PM.U r checking this with the value entered in TextBox 3/25/2008.Both are not equal even if date is same.So it returns "Date has gone".If u entered 3/30/2008.It will return the "Date is yet to come".So Convert the DateFormat using toShortDate and try this...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-25-2008, 02:52 AM
Kirubhananth Kirubhananth is offline
D-Web Programmer
 
Join Date: Feb 2008
Location: My native is Madurai but now in Chennai
Posts: 61
Kirubhananth is on a distinguished road
Send a message via AIM to Kirubhananth Send a message via Skype™ to Kirubhananth
Smile Re: Comparing two Dates

Thank you madam,

I found the error and rectified it as follows


DateTime dt = DateTime.Parse(TextBox1.Text);
Label2.Text = dt.Day.ToString();
Label3.Text = dt.Month.ToString();
Label4.Text = dt.Year.ToString();
int dat = Convert.ToInt32(Label2.Text);
int mon = Convert.ToInt32(Label3.Text);
int yr = Convert.ToInt32(Label4.Text);

DateTime current = DateTime.Now;
int datn = Convert.ToInt32(current.Day.ToString());
int monn = Convert.ToInt32(current.Month.ToString());
int yrn = Convert.ToInt32(current.Year.ToString());
Label sts = new Label();
sts.ID = "statuslbl";

DateTime dt1 = new DateTime(yr, mon, dat);
DateTime dt2 = new DateTime(yrn, monn, datn);
if (DateTime.Compare(dt1, dt2) < 0)
{
// The date was gone.
}
else if (DateTime.Compare(dt1, dt2) == 0)
{
// Equal
}
else
{
// The date is yet to come
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-25-2008, 08:44 PM
poornima poornima is offline
D-Web Sr.Programmer
 
Join Date: Dec 2007
Posts: 189
poornima is on a distinguished road
Smile Re: Comparing two Dates

Hi kirubananth,
U had solved it.Great!..To debug and know the values of the variable use F10 key for debugging ur code.Set breakpoint and check.U will rectify the error by urself..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-25-2008, 09:46 PM
Kirubhananth Kirubhananth is offline
D-Web Programmer
 
Join Date: Feb 2008
Location: My native is Madurai but now in Chennai
Posts: 61
Kirubhananth is on a distinguished road
Send a message via AIM to Kirubhananth Send a message via Skype™ to Kirubhananth
Smile Re: Comparing two Dates

Thank you poornima.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Show all the dates between two dates in YYYY-MM-DD format itbarota HTML, CSS and Javascript Coding Techniques 2 03-11-2008 09:05 PM
Which TSL functions you will use for Comparing the text? senthilkannan Testing Tools 0 07-30-2007 03:05 AM
Diff. between dates jegan PHP Programming 0 07-30-2007 03:02 AM
dates swoosh VB.NET Programming 0 03-15-2007 07:08 PM
Comparing Values pranky PHP Programming 0 02-24-2007 01:06 AM


All times are GMT -7. The time now is 08:00 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0