0

I have a database which displays dates as dd/mm/yyyy In my listview I have changed it so it displays as mm/dd/yyyy

<asp:Label ID="TPTIMEIN" runat="Server" Text='<%#Eval("TPTIMEIN", "{0: MM/dd/yyyy HH:mm:ss}") %>' />

I have another part of code which changes the font color to red if the date is more than 2 hours old

Label TimeLabel = (Label)e.Item.FindControl("TPTIMEIN");
            if (TimeLabel != null)
            {

                DateTime Total;
                if (DateTime.TryParse(TimeLabel.Text, out Total) == true)
                {
                    if (Total < DateTime.Now.AddHours(-2))
                    {

                        TimeLabel.ForeColor = System.Drawing.Color.Red;                          
                    }
                }
            }

However, here's the problem the code above only seems to work on the old format dd/mm/yyyy. So it will highlight 01/11/yyyy but not 01/14/yyyy as it's not recognizing it. How would i change this?

Hope this makes sense.....

Edit

I've tried something like this but I can't use a "<" this way

if (Total < DateTime.Now.AddHours(-2).ToString("MM.dd.yyyy"))

3 Answers 3

1

Since you already know the format you want to use you can just use DateTime.TryParseExact instead.

Change

 if (DateTime.TryParse(TimeLabel.Text, out Total) == true)

To

if(DateTime.TryParseExact(TimeLabel.Text,"MM/dd/yyyy HH:mm:ss",null, DateTimeStyles.None, out Total) == true)
Sign up to request clarification or add additional context in comments.

2 Comments

Note the date format string should be MM/dd/yyyy HH:mm:ss. In particular you've made the classic mistake of putting minutes into your date (as well as not having a time in something that measures within 2 hours). :)
Thanks for your help, this should work but I'm starting to think I'm gonna have to go and mess around with the SQL date format. When I run the app on the VB local server it will display as mm/dd/yyyy but when uploaded on the web server it goes back to dd/mm/yyyy If you have any ideas on this issue let me know!!!
1

You can use DateTime.TryParseExact and provide the dateformat you changed to. Assuming you changed the format everywhere else in your app.

Example:

   DateTime parsedDateValue;
    string date = "05/14/2014";
    DateTime.TryParseExact(date, "MM/dd/yyyy", Thread.CurrentThread.CurrentCulture, System.Globalization.DateTimeStyles.None, out parsedDateValue);
    Console.WriteLine(parsedDateValue.ToShortDateString());

//prints 5/14/2014

Comments

0

If your system date format is dd/mm/yyyy, TryParse will try to convert your string into this format.

If you have 01/14/yyyy, which is not valid as per your system time, Total will not be initialized to the date you are parsing. Instead it will initialize to default date time.

please have a consistent date formatting in all places. Either "dd/mm/yyyy" or "mm/dd/yyyy"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.