// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Globalization; namespace E { public class DateFun { private const string US = "M/d/yyyy"; private const string ISO = "yyyy/MM/dd"; private static CultureInfo ci; static DateFun() { ci = (CultureInfo)CultureInfo.CurrentCulture.Clone(); DateTimeFormatInfo dtf = (DateTimeFormatInfo)ci.DateTimeFormat.Clone(); dtf.DateSeparator = "/"; ci.DateTimeFormat = dtf; } public static string US2ISO(string s) { return DateTime.ParseExact(s, US, ci).ToString(ISO, ci); } public static string ISO2US(string s) { return DateTime.ParseExact(s, ISO, ci).ToString(US, ci); } public static void Main(string[] args) { Console.WriteLine(US2ISO("4/1/2009")); Console.WriteLine(ISO2US("2009/04/01")); } } }