Tarak
Tarak

Reputation: 1212

Format Date and Time in .NET

How to format date and time to local system date and time format? I need to format date and time separately. Suppose date is "06/11/2013" and time is "16:00:00" now both need to be formatted to system current date format and time format (with am/pm if 12 hours mode).

Upvotes: 0

Views: 126

Answers (2)

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

using System.Globalization;
string date = DateTime.Now.ToString("MM-dd-yyyy HH:mm:ss tt", CultureInfo.InvariantCulture);

Upvotes: 1

Brian Shamblen
Brian Shamblen

Reputation: 4703

DateTime.ToString() takes a string format parameter that allows you to create whatever format you want. http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

If you use DateTime.Now to get the current date/time of the server you can then use the .ToString() method to format your output.

Upvotes: 4

Related Questions