How to convert only the date without time to a string in C#

1 Answer

0 votes
using System;

class Program
{
    static string DateToString(DateTime dt) {
        return dt.ToString("yyyy-MM-dd");
    }

    static DateTime MakeDate(int y, int m, int d) {
        return new DateTime(y, m, d);
    }

    static void Main()
    {
        DateTime today = DateTime.Today;
        Console.WriteLine("Today's date is: " + DateToString(today));

        DateTime myDate = MakeDate(2025, 12, 7);
        Console.WriteLine("Hard-coded date is: " + DateToString(myDate));
    }
}



/*
run:

Today's date is: 2026-05-30
Hard-coded date is: 2025-12-07

*/

 



answered 8 hours ago by avibootz

Related questions

...