0

I have to convert this time format string (20130221191038.576375+330) to normal datetime through c# classes like datetime.

please share the idea to convert this..

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
Raja
  • 103
  • 1
  • 11
  • I assume you are converting BIGINT format to DateTime, aren't you? – Mark Feb 22 '13 at 06:00
  • Actually i got that format of string from Win32_LogonSession(wmi class) property of starttime..i dont know it is bigint or not? – Raja Feb 22 '13 at 06:03
  • 1
    You mentioned you retrieved that value from `Win32_LogonSession`, but that class has no string that represents a DateTime value. What method/call are you making to retrieve this class (please show some code)? – Erik Philips Feb 22 '13 at 06:07
  • 1
    according to http://msdn.microsoft.com/en-us/library/windows/desktop/aa394189(v=vs.85).aspx the value it returns is actually in datetime format. are you converting this to string? – Mark Feb 22 '13 at 06:08
  • 1
    According to [this](http://msdn.microsoft.com/en-us/library/windows/desktop/aa389802(v=vs.85).aspx), that is CIM_DATETIME format. I did a quick search on stack overflow for CIM_DATETIME to .NET DateTime, and [this](http://stackoverflow.com/questions/1717758/parse-cim-datetime-to-net-datetime) is what came up. The answer to the linked question only goes as far as parsing up to the period, but that should get you started – Tung Feb 22 '13 at 06:12
  • both are correct but i have to check once again i loaded the datetime into datatable and displayed into out...when convert the datetime to a string it shows like this..is it a correct datetime format string ? – Raja Feb 22 '13 at 06:19
  • If it returns a datetime format then you can format right away. – Mark Feb 22 '13 at 07:24

3 Answers3

0

Thanks guys, I got the solution for my requirement with the help of Erik,tung and BlackHatShadow.

Referred this

this also

Community
  • 1
  • 1
Raja
  • 103
  • 1
  • 11
0

The format you have is a CIM_DATETIME value, which is almost simple to parse. The only problem is that it specifies the timezone as an offset in number of minutes.

You can use the DateTime.TryParseExact to convert the portion of the string prior to the timezone specifier, then subtract the timezone value (in minutes) from the result to get the UTC datetime. Then you can convert to local time if you need, or leave it in UTC form.

public static DateTime? CIMToUTCDateTime(string CIM_DATETIME)
{
    // CIM_DATETIME must be 25 characters in length
    if (string.IsNullOrEmpty(CIM_DATETIME) || CIM_DATETIME.Length != 25)
        return null;

    // Get the datetime portion of the string without timezone offset
    string dtPortion = CIM_DATETIME.Substring(0, 21);

    // convert to datetime
    DateTime dt;
    if (!DateTime.TryParseExact(dtPortion, "yyyyMMddHHmmss.ffffff", System.Globalization.DateTimeFormatInfo.InvariantInfo, System.Globalization.DateTimeStyles.AssumeUniversal | System.Globalization.DateTimeStyles.AdjustToUniversal, out dt))
        return null;

    // subtract timezone offset to get UTC time equivalent
    int tzoffset;
    if (!Int32.TryParse(CIM_DATETIME.Substring(21), out tzoffset))
        return null;
    dt = dt.AddMinutes(-tzoffset);

    // return UTC datetime
    return dt;
}

And now that I've written this horrid little method, you've gone and found another solution. Typical :P

Corey
  • 15,524
  • 2
  • 35
  • 68
0

I know you already found a solution, but I came across this nice ManagementDateTimeConverter .Net class that does exactly what you want. All you need to do is:

// This gets converted to your local time
DateTime converted = ManagementDateTimeConverter.ToDateTime("20130221191038.576375+330")

// If you want the UTC equivalent:
converted.ToUniversalTime()
Tung
  • 5,334
  • 1
  • 34
  • 41