• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 880
  • Last Modified:

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

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

0
Camillia
Asked:
Camillia
  • 5
  • 5
  • 3
1 Solution
 
malikirfan28Commented:
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

0
 
brutaldevCommented:
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

0
 
CamilliaAuthor 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.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
CamilliaAuthor Commented:
just saw your answer about if-else , let me try that
0
 
brutaldevCommented:
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.
0
 
CamilliaAuthor Commented:
This line wont compile...shouldnt it be datatetime.today.addday(1)

DateTime.Today + 1
0
 
malikirfan28Commented:
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;
            }
0
 
brutaldevCommented:
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

0
 
CamilliaAuthor 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??
0
 
brutaldevCommented:
You can actually use malikirfan28's solution or mine (second code sample) which both avoid the time entirely when doing the comparison.
0
 
malikirfan28Commented:
DateTime.Today also does not have minutes, seconds in it and so in this case you can use the codition like this

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;
            }
0
 
CamilliaAuthor Commented:
There's no DateTime.Date

I will try malikirfan28's code
0
 
brutaldevCommented:
Sorry been rushing through these, as malikirfan28 said DateTime.Today has no time, DateTime.Now includes the time. His solution works nicely.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

  • 5
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now