Rashim's Blog

Archive for August 2012

Today i have done a very little but  interesting thing “Capture and notify the change of date and time format in OS“.

There are some date pickers in our project and we need to display the date time (Backup/Restore Date Time) in some reports. So the requirement was that, the date time format should be as like the OS date time format and if anyone changes the format of date time of the OS, it will reflect on all the date time formats which has been used on whole project. This is a pretty easy stuff though. By using the SystemEvents Class we can do it easily. Do you wanna to know more about this event, please have a look on this link?

class Program
    static void Main(string[] args)
        Console.WriteLine("Initial DateTime format :" + CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.ToString(CultureInfo.InvariantCulture));

    private static void Initialize()
        Microsoft.Win32.SystemEvents.UserPreferenceChanged +=new Microsoft.Win32.UserPreferenceChangedEventHandler(SystemEventsUserPreferenceChanged);

    private static void SystemEventsUserPreferenceChanged(object sender, Microsoft.Win32.UserPreferenceChangedEventArgs e)
        var format = (DateTimeFormatInfo)CultureInfo.CurrentCulture.GetFormat(formatType: typeof(DateTimeFormatInfo));

        if (format != null)
            if (format.ShortDatePattern != null)
                Console.WriteLine(value: "Current DateTime format :" +format.ShortDatePattern.ToString(CultureInfo.InvariantCulture));

Keep running this console app while changing the date time format of your OS. You will see the latest date time format in the console. Enjoy it 🙂

Blog Stats

  • 189,209 hits


%d bloggers like this: