-1

I need to be able to convert into days the long datetime string returned by the following wmi query:

SelectQuery query = new SelectQuery("SELECT * FROM Win32_NetworkLoginProfile");

The propety name is PasswordAge. PasswordAge Data type: datetime

Length of time a password has been in effect. This value is measured from the number of seconds elapsed since the password was last changed.

Example: 00000011171549.000000:000

ManagementScope scope = CreateNewManagementScope(computerName);
SelectQuery query = new SelectQuery("SELECT Name, PasswordAge FROM Win32_NetworkLoginProfile WHERE Privileges=2 ");
try
{
    using (var searcher = new ManagementObjectSearcher(scope, query))
    {
        ManagementObjectCollection accounts = searcher.Get();
        List<string> accountNames = new List<string>();
        foreach (ManagementObject account in accounts)
        {
            string name = account["Name"].ToString();
            string pAge = account["PasswordAge"].ToString();

            accountNames.Add(name + " " + pAge);
         }
         lstAccounts.DataSource = accountNames;
     }
}
Sam
  • 265
  • 1
  • 3
  • 9
  • have you tried DateTime.FromOADate Method? – Jegadeesh Mar 12 '14 at 13:29
  • The question is inconsistent. Convert into days the datetime. Length of time in effect in seconds. – paparazzo Mar 12 '14 at 13:35
  • I tried this: `var t = DateTime.FromOADate(Convert.ToDouble(account["PasswordAge"]));` and get exception error input string was not in correct format. – Sam Mar 12 '14 at 13:39

3 Answers3

1

First, parse the string to create an integer or long. Create a TimeSpan from the result, then get the Days property of that object:

var s = (long)Double.Parse(pAge);
var t = TimeSpan.FromSeconds(s);
Console.WriteLine(t.Days);

Note that the Days property is an integer. It represents the number of whole days in the TimeSpan. If you need to be more precise, include the number of hours as well, or seconds etc.

Also note that the example you gave (19521201000230.000000 seconds) represents about 619,000 years. My guess is that this is the default value returned by the query when a user has never changed their password. I'm bringing this up because it's longer than the max period of time that can be represented by a TimeSpan (about 29,000 years) so this code won't work for the default value.

vlad
  • 4,748
  • 2
  • 30
  • 36
  • 1
    @Sam downvotes aren't useful unless you explain them. I can't help you if I don't know why this answer doesn't work. – vlad Mar 12 '14 at 13:23
  • @Sam my apologies, I should not have assumed. Either way, edited to use the variable names you provided, and added more detail. – vlad Mar 12 '14 at 13:37
  • This is the value I'm getting for pAge: 00000000005058.000000:000 Exception error at: `var s = (long)Double.Parse(pAge);` – Sam Mar 12 '14 at 13:53
  • @Sam that's because the string isn't really a double. You can address this one of two ways: if you need the precision of the fractional seconds, trim `pAge` after the semicolon: `pAge.Substring(0, pAge.IndexOf(':'));`; if you don't need that precision, trim the string after the decimal point. That would allow you to use a `long` for parsing directly, as opposed to going through a `double` – vlad Mar 12 '14 at 14:43
0

Divide it by 1000*60*60*24 = 86400000?

ttaaoossuuuu
  • 7,786
  • 3
  • 28
  • 58
  • please add more information about your suggestion, such as how you are solving the problem, and what the op was doing wrong. – Our Man in Bananas Mar 12 '14 at 13:21
  • he simply took the long multiplied it by 1000 to get seconds then multiplied it by 60 to get minutes then multiplied it by 60 again to get hours then lastly multiplied it by 24 to get days. So basically there is `86400000` milliseconds in a day. So as @Taosique said, divide the long by 86400000 and you will have days... – string.Empty Mar 12 '14 at 13:40
  • @Tosique that was an example of the string format, an actual result I'm getting is: 00000000005058.000000:000 – Sam Mar 12 '14 at 14:45
0

I know this is late - but this question was referred to as a possible duplicate of another question (what is this format for seconds XXXXXXX.XXXXX:XXX (Win32_NetworkLoginProfile)). The actual format is documented here : https://msdn.microsoft.com/en-us/library/aa390895(v=vs.85).aspx

Community
  • 1
  • 1
PaulF
  • 6,673
  • 2
  • 18
  • 29