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
Solved

Comparing dates in C# .NET

Posted on 2004-09-07
6
12,721 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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!
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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