We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Check for today's date and tomorrow's date in switch

Camillia
Camillia asked
on
Medium Priority
909 Views
Last Modified: 2012-05-11
I have a date field and I want to check if it's today or tomorrow. My code below doesnt work...switch needs a constant text... how can I do this?
switch (DateTime.Parse(row["expectedarrivaldate"].Text))
            {
                case DateTime.Today :
                    row.BackColor = System.Drawing.Color.White;
                    break;
                case DateTime.Today + 1 :
                    row.BackColor = System.Drawing.Color.White;
                    break;
                default:
                    row.BackColor = System.Drawing.Color.Silver;
                    break;
            }

Open in new window

Comment
Watch Question

Check it please.

switch (DateTime.Parse(row["expectedarrivaldate"].Text))
            {
                case DateTime.Today:
                    row.BackColor = System.Drawing.Color.White;
                    break;
                case DateTime.Today.AddDays(1):
                    row.BackColor = System.Drawing.Color.White;
                    break;
                default:
                    row.BackColor = System.Drawing.Color.Silver;
                    break;
            } 

Open in new window

You're going to have to use a if/else if here, case statements only work on constant values, adding a + 1 to the date is dynamic so it won't compile.
 
DateTime myDate = DateTime.Parse(row["expectedarrivaldate"].Text);

if (myDate == DateTime.Today || myDate == DateTime.Today + 1)
{
  row.BackColor = System.Drawing.Color.White;
}
else
{
  row.BackColor = System.Drawing.Color.Silver;
}

Open in new window

CERTIFIED EXPERT

Author

Commented:
No, for this line : switch (DateTime.Parse(row["expectedarrivaldate"].Text))
I get this error
A value of an integral type expected

For case DateTime.Today:
 and case DateTime.Today.AddDays(1):

I get A constant value is expected error.
CERTIFIED EXPERT

Author

Commented:
just saw your answer about if-else , let me try that
The switch with DateTime was the beginning of your problems. Integral types include int, long, byte, double etc as well as string (which isn't "really" an integral type but .NET treats it that way). DateTime is a struct and as such cannot be used as a constant value in the switch statement.
CERTIFIED EXPERT

Author

Commented:
This line wont compile...shouldnt it be datatetime.today.addday(1)

DateTime.Today + 1
TimeSpan ts = DateTime.Today.Subtract(DateTime.Parse(row["expectedarrivaldate"].Text));
            if (ts.TotalDays == 0 || ts.TotalDays == 1)
            {
                row.BackColor = System.Drawing.Color.White;
            }
            else
            {
                row.BackColor = System.Drawing.Color.Silver;
            }
Yes, sorry, copy and paste fail :/

You should also be careful when you compare dates like this as the ticks are actually compared (day, time and seconds/milliseconds included). If you want a more general check use DateTime.Date to avoid the time and get more accurate conditional results.
 
DateTime myDate = DateTime.Parse(row["expectedarrivaldate"].Text);

if (myDate.Date == DateTime.Date ||
    myDate.Date == DateTime.Date.AddDays(1))
{
  row.BackColor = System.Drawing.Color.White;
}
else
{
  row.BackColor = System.Drawing.Color.Silver;
}

Open in new window

CERTIFIED EXPERT

Author

Commented:
I dont want the time and seconds. My vale from the database is like this:
2011-04-26 00:00:00.000

with no time,seconds,milliseconds..i should use what??
You can actually use malikirfan28's solution or mine (second code sample) which both avoid the time entirely when doing the comparison.
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Author

Commented:
There's no DateTime.Date

I will try malikirfan28's code
Sorry been rushing through these, as malikirfan28 said DateTime.Today has no time, DateTime.Now includes the time. His solution works nicely.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.