ManagementDateTimeConverter in C# .Net windows application

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DKn
    New Member
    • Aug 2007
    • 53

    ManagementDateTimeConverter in C# .Net windows application

    Hello All,

    I am having an C#.Net windows application.The re I am using WMI class Win32_Operating System to get the information of operating system. In this class we are having a Parameter Called "LocalDateTime" .
    If the time zone is "(GMT+5:30) chennai,kolkata ,Mumbai,Newdelh i" we will get localdatetime from WMI as "20070327182923 .82800+330" . This value I have converted to datetime by using ManagementDateT imeConverter
    DateTime Dt;
    Dt = ManagementDateT imeConverter.To DateTime
    string strFormatedDate = Dt.ToString("MM M/dd/yyyy hh:mm:ss tt");
    ("2007032718292 3.82800+330"); This is working fine and giving the result like Mar/27/2008 06:29:23

    the problem comes If I change the Time zone, Then i tried get the localdatetime dynamically , WMI is returning the correct value , but the ManagementDateT imeConverter is not giving the correct value.It is giving the previous time zone value only.

    But when i close the appliaction and run it again then it is giving current timezone time what ever we have selected.

    What is the problem here? can anyone give any solution to get current time zone time.

    Thanks in advance...
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Why not just use DateTime.Parse( ) ?

    DateTime dt = DateTime.Parse( "20070327182923 .82800+330" );

    Should give you a datetime object of the correct value.



    Also, there seems to be some CLR caching involved with timezone information.
    You can do something like the following to reset what the cached timezone is.

    Code:
    try
    {//System.Reflection.
       FieldInfo fi = typeof(TimeZone).GetField("currentTimeZone", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
       fi.SetValue(null, null);
    }
    catch (Exception ee)
    {
    }

    Comment

    Working...