Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Apr 23, 2012 08:18 AM by Saesee
Apr 18, 2012 02:57 PM|LINK
I have an Access table with members informations and a column "DateNaissance" in
DateTime format where stored the Birth Date of the member (exemple : 29/01/1985)
On my Default page, i want to show Members who have they anniversary in the week. I try to show 5 days before anniversary a text like :
"Anniversary of Member-A in X days."
and the D-Day : "Today, it's the anniversary of Member-A ! Happy Anniversary !"
My problem is that I don't understand how work the
DateTime.Compare and how to use it for show my anniversaries.
Someone can help me please ? Thanks by advance
- Webmatrix 1 / Razor CSHTML C#
Apr 18, 2012 03:28 PM|LINK
Run the code below to get a feel for the DateTime capabilities in C#. DateTime.Compare only tells you whether one date is earler, the same, or later than another date.
var today = DateTime.Now;
var yesterday =today.AddDays(-1);
var tomorrow = today.AddDays(+1);
<p>Date and Time Yesterday, today, tomorrow: @yesterday @today @tomorrow
<p>Date Only Yesterday, today, tomorrow (time goes to 12:00 AM): @yesterday.Date @today.Date @tomorrow.Date</p>
<p>Compare yesterday to today yields: @DateTime.Compare(yesterday, today)</p>
<p>Compare today to today yields: @DateTime.Compare(today, today)</p>
<p>Compare tomorrow to today yields: @DateTime.Compare(tomorrow, today)</p>
<p>Today is the anniversary of: @today.AddYears(-1)</p>
Apr 18, 2012 05:03 PM|LINK
I'll try this tomorrow & back to solve it ^^
Apr 18, 2012 10:04 PM|LINK
I've tried to solve your problem:
var message = "";
DateTime dt1 = Convert.ToDateTime(Request["firstdate"]);
DateTime dt2 = Convert.ToDateTime(Request["seconddate"]);
message = "Anniversary of Member-A is too late.";
// Compute the next anniversary
DateTime cd = dt1.AddYears(dt2.Year - dt1.Year);
cd = (cd.CompareTo(dt2) < 0 ? dt1.AddYears(dt2.Year - dt1.Year + 1) : cd);
if (cd.CompareTo(dt2) == 0)
message = "Today, it's the anniversary of Member-A! Happy Anniversary!" ;
var diff = cd.Subtract(dt2).Days;
if (diff < 5)
message = "Anniversary of Member-A in " + diff.ToString() + " days.";
<meta charset="utf-8" />
<form action="" method="post">
Anniversary (dd/mm/yyyy): <input type="text" name="firstdate" /><br />
Today Date (dd/mm/yyyy): <input type="text" name="seconddate" />
<input type="submit" value="submit"/>
Hope it helps.
Apr 23, 2012 08:18 AM|LINK
Sorry for the time to answer.
Thanks GmGregori, it's very helpful; now I understand how to compare my dates.