How to remove time portion of date in C# in DateTime object only?

后端 未结 30 3286
醉话见心
醉话见心 2020-11-22 09:08

I need to remove time portion of date time or probably have the date in following format in object form not in the form of string.

         


        
相关标签:
30条回答
  • 2020-11-22 09:49

    Have a look at the DateTime.Date property.

    Gets the date component of this instance.

    0 讨论(0)
  • 2020-11-22 09:50

    in my experience none of the said solutions worked, maybe because I wanted to remove the time from extracted date from database, but the code below worked fine:

    var date = target_date.Value.ToString("dd/MM/yyyy"); 
    
    0 讨论(0)
  • 2020-11-22 09:51

    The easiest way is something like this and it will return only the date:

    var date = DateTime.Now.ToShortDateString();
    
    0 讨论(0)
  • 2020-11-22 09:52

    Use the Date property:

    var dateAndTime = DateTime.Now;
    var date = dateAndTime.Date;
    

    The date variable will contain the date, the time part will be 00:00:00.

    0 讨论(0)
  • 2020-11-22 09:52

    If you are converting it to string, you can easily do it like this.

    I'm taking date as your DateTime object.

    date.ToString("d");
    

    This will give you only the date.

    0 讨论(0)
  • 2020-11-22 09:52

    Declare the variable as a string.

    example :

    public string dateOfBirth ;
    

    then assign a value like :

    dateOfBirth = ((DateTime)(datetimevaluefromDB)).ToShortDateString();
    
    0 讨论(0)
提交回复
热议问题