Rashim's Blog

Capture and Notify the change of date and time format in OS

Posted on: August 6, 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)
    {
        Initialize();
        Console.WriteLine("Initial DateTime format :" + CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.ToString(CultureInfo.InvariantCulture));
        Console.ReadLine();
    }

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

    private static void SystemEventsUserPreferenceChanged(object sender, Microsoft.Win32.UserPreferenceChangedEventArgs e)
    {
        Thread.CurrentThread.CurrentCulture.ClearCachedData();
        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 🙂

Advertisements

4 Responses to "Capture and Notify the change of date and time format in OS"

small but powerful……..

Thanks

its really interesting…

Bro great one…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: