Solved

Comparing dates in C# .NET

Posted on 2004-09-07
6
12,712 Views
Last Modified: 2012-06-27
Hi,

I need some SIMPLE CODE which will:

Get the "current date" of today, then compare it to  another date (actually it will be compared to a dataset/datatable of them, but I'll sort that out!), and I need to find any that are "1 day" away.

i.e. If today is: 7/9/2004

    I want the code to find the date that is 8/9/2004.

Im using it in my program so it will always inform (REMIND!!) the user that they have an appointment the next day.

Thanks!
0
Comment
Question by:rad_001
6 Comments
 
LVL 19

Expert Comment

by:Desp
Comment Utility
use the AddDays(n) Function
DateTime d1 = DateTime.Now;
DateTime d2 = d1.AddDays(1);
0
 
LVL 9

Expert Comment

by:tinchos
Comment Utility
try with

DateTime d1 = DateTime.Now;
DateTime d2 = %AppointMentDate%;              // in d2 goes the appointmentDate

TimeSpan elapsed = d2.Subtract( d1 );

if( elapsed.TotalDays = 1 )
     // Do something

Hope this helps

Tincho
0
 
LVL 5

Expert Comment

by:tzxie2000
Comment Utility
we may create an method like

public bool HasAppoint(DateTime dt[])//dt[]is all the appoint select out from dataset
{
   DateTime today = DateTime.Now;

  DateTime tomorrow=today.AddDays(1);

  for(i=0;i<dt.Length;i++)
 {
   if(tomorrow.date.Equal(dt[i].date)) break;
 }
 return i>=dt.Lenth;
}

but I think we may use sql to do this as normally you the better show any appointments tomorrow if the customer has any
0
 
LVL 20

Accepted Solution

by:
TheAvenger earned 500 total points
Comment Utility
DateTime appDate = something; // Coming from the db or whatever
DateTime tomorrow = DateTime.Today.AddDays (1);
if (tomorrow == appDate.Date)  // Compare only the days, not the time
  // BINGO

Note that a solution like the one presented by tinchos :
if( elapsed.TotalDays = 1 )
     // Do something

will have a problem that also dates which are 1 month and 1 day away will also be found
0
 
LVL 23

Expert Comment

by:rama_krishna580
Comment Utility
Hi,

Look at here...it may help you..

========================================================
Dim dtToday as Date = Date.Today
Dim dtAnotherDate as Date = cDate("7/28/04")
Dim Delta as Integer = DateDiff(DateInterval.Day, dtToday, dtAnotherDate)

If Delta > 12 then
    MessageBox.Show("That Date is more that 12 days in the future")
Else
    MessageBox.Show("That date is either in the past, or less than 12 days in the future")
End If

========================================================
          Dim dtCompareDate1 As Date = Now.Date.Today
          Dim dtCompareDate2 As Date = Convert.ToDateTime("7 / 13 / 2004")
          Dim dateDifference As Integer = DateDiff(DateInterval.Day, dtCompareDate1, dtCompareDate2)
          If dateDifference < 13 And dateDifference > 0 Then
          MessageBox.Show("Between 12 days")
          ElseIf dateDifference < 0 Then
               MessageBox.Show("PassDue")
          End If
========================================================

i hope it may help you..
best of luck...

R.K
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now