Example

 using System; using System.Globalization; namespace Samples { public class DateTimeSample { public static void Main() { DateTime dt = DateTime.Now; string[] formats = {"d", "D", "f", "F", "g", "G", "m", "r", "s", "t", "T", "u", "U", "y", "dddd, MMMM dd yyyy", "ddd, MMM d \"'\"yy", "dddd, MMMM dd", "M/yy", "dd-MM-yy"}; foreach(string format in formats) { Console.WriteLine("Displayed using format {0} is: {1}", format, dt.ToString(format, DateTimeFormatInfo.InvariantInfo)); } DateTime d = new DateTime(1959, 9, 3); DateTime b = new DateTime(1973, 9, 25); Console.WriteLine("Today is {0:F}", dt); TimeSpan age = dt - d; Console.WriteLine("Damien is: {0:F}", age); Console.WriteLine("He has lived: {0,2:F} days", age.TotalDays); age = dt - b; ...

Get .NET Framework Standard Library Annotated Reference, Volume 1: Base Class Library and Extended Numerics Library now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.