?
Solved

Comparing dates in C# .NET

Posted on 2004-09-07
6
Medium Priority
?
12,729 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:Fahad Mukhtar
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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

765 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