Solved

Comparing dates in C# .NET

Posted on 2004-09-07
6
12,723 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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