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

后端 未结 30 3326
醉话见心
醉话见心 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:33

    Here is another method using String.Format

        DateTime todaysDate = DateTime.UtcNow;
    
        string dateString = String.Format("{0:dd/MM/yyyy}", todaysDate);
    
        Console.WriteLine("Date with Time: "+ todaysDate.ToString());
    
        Console.WriteLine("Date Only : " + dateString);
    

    Output:

    Date with Time: 9/4/2016 11:42:16 AM
    
    Date Only : 04/09/2016
    

    This also works if the Date Time is stored in database.

    For More Date and Time formatting check these links:

    Reference 1

    Reference 2

    Hope helps.

提交回复
热议问题