How to get current date and time in C#

1 Answer

0 votes
using System;

public class Program
{
    public static void Main()
    {
        string s = string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now);
        Console.WriteLine(s);

        DateTime datetime = DateTime.Now;
        s = datetime.ToString("yyyy-MM-dd HH:mm:ss");
        Console.WriteLine(s);

        Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
    }
}




/*
run:

2022-03-26 13:58:04
2022-03-26 13:58:04
2022-03-26 13:58:04

*/

 



answered Mar 26, 2022 by avibootz
...