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.
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>
<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>
}
Saesee
Member
33 Points
26 Posts
Compare DateTime to show 1 week anniversary
Apr 18, 2012 02:57 PM|LINK
Hi,
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#
RAZOR csharp
rrrsr7205
Participant
1304 Points
313 Posts
Re: Compare DateTime to show 1 week anniversary
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> <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> }Saesee
Member
33 Points
26 Posts
Re: Compare DateTime to show 1 week anniversary
Apr 18, 2012 05:03 PM|LINK
Thanks,
I'll try this tomorrow & back to solve it ^^
GmGregori
Contributor
5470 Points
737 Posts
Re: Compare DateTime to show 1 week anniversary
Apr 18, 2012 10:04 PM|LINK
I've tried to solve your problem:
@{ var message = ""; if (IsPost) { 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!" ; } else { var diff = cd.Subtract(dt2).Days; if (diff < 5) { message = "Anniversary of Member-A in " + diff.ToString() + " days."; } } } } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title></title> </head> <body> <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"/> </form> <p>@message</p> </body> </html>Hope it helps.
Saesee
Member
33 Points
26 Posts
Re: Compare DateTime to show 1 week anniversary
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.