You can use basic addition and subtraction with DateTime objects to yield TimeSpan objects, which can be used with comparisons and more.
DateTime a = DateTime.Now;
DateTime b = DateTime.Now.AddDays(-6);
if (a >= b)
{
//Add class
}
else
{
//Use the diff to check the TimeSpan difference
if ((b - a).Days >= 30)
{
//Do work
}
}
For your purposes :
DateTime _Now = DateTime.Now;
Label DueDate = (Label)childItem.FindControl("OrderDueDateLabel");
DateTime _DueDate = Convert.ToDateTime(DueDate.Text);
//If the Due Date has passed
if (_DueDate < _Now)
{
//If the Due Date is over 30 days old
if((_Now - _DueDate).Days > 30)
{
//Apply 30 Days Styling
}
//Due Date is less than 30 days old
else
{
DueDate.CssClass = "w100px red";
}
}
If you wanted to have some fun with a really small version, you could use :
if (_DueDate < _BitOld) SendTheDidYouOverlookMessage();
watch out how you structure your if statements because if _DueDate is prior to _GettingWorried it will also be prior to _BitOld and you probably don't want to contact them twice with different messages.
(Note that I use Today rather than Now, it represents the midnight which has just gone past rather than the exact split millisecond when you happened to run the code which is what Now gives you. No big deal but it prevents confusion if _DueDate actually
has a time component as well, if you run the code in the middle of the day then invoices due in the moring are picked up but those in the afternoon are not)
Bobby-Z
Contributor
2838 Points
1120 Posts
Date Difference
Jan 14, 2013 02:50 PM|LINK
I am trying to determine a date difference in C# fro 30 60 90 etc...
I am reading a label control in a listview, and changing CssClass based upon due date
DateTime _Now = DateTime.Now; Label DueDate = (Label)childItem.FindControl("OrderDueDateLabel"); DateTime _DueDate = Convert.ToDateTime(DueDate.Text); if (_DueDate < _Now) { DueDate.CssClass = "w100px red"; }So I have text changing to red if the due date is greater than todays date, but I want to also change cssClass if it is greater than XX days
if (_DueDate < _Now)
{
Calculate Time Difference Variable here
if (_DueDate < _Dif30Days)....
Thank-You
Currently Learning: ASP, SQL, CSS, JavaScript, AJAX, XML, XSLT, C# So please be patient with me! Thanks
budugu
All-Star
41132 Points
6021 Posts
Re: Date Difference
Jan 14, 2013 02:54 PM|LINK
Use AddDays Method
if (_DueDate < _Now.AddDays(-10))"Don't be afraid to be wrong; otherwise you'll never be right."
Rion William...
All-Star
27780 Points
4588 Posts
Re: Date Difference
Jan 14, 2013 02:55 PM|LINK
You can use basic addition and subtraction with DateTime objects to yield TimeSpan objects, which can be used with comparisons and more.
DateTime a = DateTime.Now; DateTime b = DateTime.Now.AddDays(-6); if (a >= b) { //Add class } else { //Use the diff to check the TimeSpan difference if ((b - a).Days >= 30) { //Do work } }For your purposes :
DateTime _Now = DateTime.Now; Label DueDate = (Label)childItem.FindControl("OrderDueDateLabel"); DateTime _DueDate = Convert.ToDateTime(DueDate.Text); //If the Due Date has passed if (_DueDate < _Now) { //If the Due Date is over 30 days old if((_Now - _DueDate).Days > 30) { //Apply 30 Days Styling } //Due Date is less than 30 days old else { DueDate.CssClass = "w100px red"; } }If you wanted to have some fun with a really small version, you could use :
if(_DueDate < _Now) { DueDate.CssClass = ((_Now-_DueDate).Days > 30) ? "your > 30 day style" : "w100px red"; }(which could actually be even smaller if we had all of the information you needed)
oned_gk
All-Star
31760 Points
6489 Posts
Re: Date Difference
Jan 14, 2013 02:57 PM|LINK
DateTime _Now = DateTime.Now; Label DueDate = (Label)childItem.FindControl("OrderDueDateLabel"); DateTime _DueDate = Convert.ToDateTime(DueDate.Text); if (_DueDate < _Now) { DueDate.CssClass = "w100px red"; } if (_DueDate.AddDays(30) < _Now) { } if (_DueDate.AddDays(60) < _Now) { } if (_DueDate.AddDays(90) < _Now) { }Paul Linton
Star
13431 Points
2535 Posts
Re: Date Difference
Jan 14, 2013 08:16 PM|LINK
Make some varaibles which represent the dates you are interested in and then use them the same way you have used _Now.
if (_DueDate < _BitOld) SendTheDidYouOverlookMessage();
watch out how you structure your if statements because if _DueDate is prior to _GettingWorried it will also be prior to _BitOld and you probably don't want to contact them twice with different messages.
(Note that I use Today rather than Now, it represents the midnight which has just gone past rather than the exact split millisecond when you happened to run the code which is what Now gives you. No big deal but it prevents confusion if _DueDate actually has a time component as well, if you run the code in the middle of the day then invoices due in the moring are picked up but those in the afternoon are not)
Bobby-Z
Contributor
2838 Points
1120 Posts
Re: Date Difference
Jan 17, 2013 12:59 AM|LINK
Like it Paul :) you forgot one
DateTime _WereGonnaBreakYourLegs = _Now.AddDays(-120);
Currently Learning: ASP, SQL, CSS, JavaScript, AJAX, XML, XSLT, C# So please be patient with me! Thanks
Rion William...
All-Star
27780 Points
4588 Posts
Re: Date Difference
Jan 17, 2013 01:03 AM|LINK
@Bobby-Z
LOL!