locked
Having Date Problem RRS feed

  • Question

  • Hi,

    I struck at simple problem in the C# date formatting.

    I just want to check Today's date against DateTimePicker's date value. I don't want to check the time of the date at all. However, I always getting time stamp along with the Today's date and DateTimePicker's value. How to get the Current Date and DateTimePicker value without Time.

    I tried TryParseExact, TryParse methods to show only Date (without Time), but it did not work

    Following is my code:


    DateTime Process_Start_date = myDateTimePicker.Value;
    DateTime Today_Date = DateTime.Today.Date; //HOW TO GET ONLY DATE; without TIME?;
    //I Tried with this:
    ////DateTime Today_Date = DateTime.TryParseExact(Today_Date_String, "dd/MM/yyyy", provider);

     if (Process_Start_date > Today_Date)
                {
                    return ;
                }

    Please help me on this?



    Violence never brings Peace. Non-Violence never brings solutions
    • Moved by Peter Ritchie Wednesday, September 3, 2008 3:53 PM Question regarding type in BCL (Moved from Visual C# General to .NET Base Class Library)
    Wednesday, September 3, 2008 3:50 PM

Answers

  • 1. DateTime always has a time with it, regardless.  The Date property of DateTime resets the time to 12:00AM regardless.  To compare two dates for equality without respect for time, use the Date property, so you're correct here, even if you don't know you are.

    2. Try DateTime.Now.ToShortDateString(); to format it properly.
    David Morton - http://blog.davemorton.net/
    • Marked as answer by Rajeeshun Wednesday, September 3, 2008 4:10 PM
    Wednesday, September 3, 2008 3:53 PM

All replies

  • 1. DateTime always has a time with it, regardless.  The Date property of DateTime resets the time to 12:00AM regardless.  To compare two dates for equality without respect for time, use the Date property, so you're correct here, even if you don't know you are.

    2. Try DateTime.Now.ToShortDateString(); to format it properly.
    David Morton - http://blog.davemorton.net/
    • Marked as answer by Rajeeshun Wednesday, September 3, 2008 4:10 PM
    Wednesday, September 3, 2008 3:53 PM
  • Thanks a lot. I added date property to the DateTimePicker's value and tested. It is working now

    Following is the code:

    String Today_Date_String = DateTime.Now.Date.ToShortDateString().ToString();<br>DateTime Process_Start_date = dtt.Value.Date; 
     
    //I set the DateTimePicker's Value to Today's date 
     
    if (Process_Start_date > Today_Date) 
     { 
     return false
     } 

    Thank you again


    Cheers,

    Rajeeshun



    Violence never brings Peace. Non-Violence never brings solutions
    Wednesday, September 3, 2008 4:13 PM