skip to Main Content

my goal => 5/26/2022
but I can’t remove time from date. => 5/26/2022 12:00:00 AM

How can I remove the time without using ToShortDateString()?

Value.Date.ToShortDateString()

Example;
https://dotnetfiddle.net/xWELRP

3

Answers


  1. You could try with

       DateTime.Now.Date
    

    if your variable is type of DateTime,itwould have hour minute and second by default,it would set the time to 0:00:00

    Login or Signup to reply.
  2. If you are using .Net6, then you can use DateOnly struct,

    var dateOnly = DateOnly.FromDateTime(DateTimeOffset.Now.Date);
    Console.WriteLine(dateOnly);
    

    .Net Fiddle

    Login or Signup to reply.
  3. You can just write like this:

    Console.WriteLine($"{data:d}");
    

    There are a few other format constraint keywords where you can find the one that works best for you, this is a very easy way to do it.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search