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

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

    None of the above answers solved my problem on winforms.

    the easiest way to reach ONLY date is the simple function in Datetime:

    DateTime dt = DateTime.now;
    String BirthDate = dt.ToShortDateString();
    

    You will only have date in Birthday string .

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

    I'm surprised no one has mentioned DateTime.Today

    var date = DateTime.Today;
    // {7/1/2014 12:00:00 AM}
    

    See MSDN

    0 讨论(0)
  • 2020-11-22 09:39
    string dt = myCalender.SelectedDate.ToString();
    string date = dt.Remove(10);
    displayDate.Content = date;
    

    If you take date from calender, with this we also get time. Which is not required all time. Using this we can remove time from date.

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

    You can use this simple code below.

    Code: DateTime.Now.ToShortDateString();

    Ex. Console.WriteLine(DateTime.Now.ToShortDateString());

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

    You can use format strings to give the output string the format you like.

    DateTime dateAndTime = DateTime.Now;
    Console.WriteLine(dateAndTime.ToString("dd/MM/yyyy")); // Will give you smth like 25/05/2011
    

    Read more about Custom date and time format strings.

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

    For using by datalist, repeater.. in aspx page:<%# Eval("YourDateString").ToString().Remove(10) %>

    0 讨论(0)
提交回复
热议问题