[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-04-26
13
Medium Priority
?
856 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

0
Comment
Question by:Camillia
  • 5
  • 5
  • 3
13 Comments
 
LVL 4

Expert Comment

by:malikirfan28
ID: 35469491
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
 
LVL 11

Expert Comment

by:brutaldev
ID: 35469498
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
 
LVL 7

Author Comment

by:Camillia
ID: 35469521
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 7

Author Comment

by:Camillia
ID: 35469585
just saw your answer about if-else , let me try that
0
 
LVL 11

Expert Comment

by:brutaldev
ID: 35469603
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
 
LVL 7

Author Comment

by:Camillia
ID: 35469604
This line wont compile...shouldnt it be datatetime.today.addday(1)

DateTime.Today + 1
0
 
LVL 4

Expert Comment

by:malikirfan28
ID: 35469620
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
 
LVL 11

Expert Comment

by:brutaldev
ID: 35469656
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
 
LVL 7

Author Comment

by:Camillia
ID: 35469671
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
 
LVL 11

Expert Comment

by:brutaldev
ID: 35469741
You can actually use malikirfan28's solution or mine (second code sample) which both avoid the time entirely when doing the comparison.
0
 
LVL 4

Accepted Solution

by:
malikirfan28 earned 2000 total points
ID: 35469779
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
 
LVL 7

Author Comment

by:Camillia
ID: 35470128
There's no DateTime.Date

I will try malikirfan28's code
0
 
LVL 11

Expert Comment

by:brutaldev
ID: 35471120
Sorry been rushing through these, as malikirfan28 said DateTime.Today has no time, DateTime.Now includes the time. His solution works nicely.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Screencast - Getting to Know the Pipeline
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question