Solved

Comparing dates in C# .NET

Posted on 2004-09-07
6
12,720 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
ID: 11996271
use the AddDays(n) Function
DateTime d1 = DateTime.Now;
DateTime d2 = d1.AddDays(1);
0
 
LVL 9

Expert Comment

by:tinchos
ID: 11996330
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
ID: 11996544
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
ID: 11996910
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
ID: 11997586
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

813 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

18 Experts available now in Live!

Get 1:1 Help Now