DateTime dt = DateTime.Now; // or every other DateTime object
string formattedDateTime = dt.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
The CultureInfo.InvariantCulture parameter ensures that the format is acceptable for all areas and never depending on the present tradition or area settings of the pc operating this system.
You may modify the format string to show the DateTime object in a special format in line with your wants. For instance, if you wish to embody the time zone offset, you should utilize the “zzz” format specifier like this:
string formattedDateTime = dt.ToString("yyyy-MM-dd HH:mm:ss zzz", CultureInfo.InvariantCulture);