CSharp - DateTime DateTimeOffset Formatting Parsing

Introduction

Calling ToString on a DateTime formats the result as a short date followed by a long time. For example:

11/11/2015 11:50:30 AM

The operating system's control panel determines such things as whether the day, month, or year comes first, the use of leading zeros, and whether 12- or 24-hour time is used.

Calling ToString on a DateTimeOffset returns offset as well:

11/11/2015 11:50:30 AM -06:00

ToShortDateString and ToLongDateString methods return just the date portion.

The long date format is also determined by the control panel.

ToShortTimeString and ToLongTimeString return just the time portion.

Demo

using System;
class MainClass{// w  w w .  j  av  a 2s .  c o  m
   public static void Main(string[] args){
      DateTime dt = new DateTime (2000, 2, 3, 10, 20, 30);
      
      Console.WriteLine(dt.ToString());
      Console.WriteLine(dt.ToShortDateString());
      Console.WriteLine(dt.ToLongDateString());
      Console.WriteLine(dt.ToShortTimeString());
      Console.WriteLine(dt.ToLongTimeString());
   }
}

Result