How to convert long datetime string to days in C #

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

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

The proforma name is PasswordAge. PasswordAge Data Type: datetime

Password expiration date. This value is measured from the number of seconds elapsed since the last password change.

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; } } 
-one
c # datetime wmi
source share
3 answers

Parse the string first to create an integer or long . Create a TimeSpan from the result, then get the Days property of this 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 TimeSpan . If you need to be more precise, indicate the number of hours, or seconds, etc.

Also note that the example you gave (19521201000230.000000 seconds) is about 619,000 years. I assume this is the default value returned by the request when the user never changed his password. I do this because it is longer than the maximum time period that TimeSpan can TimeSpan (about 29,000 years), so this code will not work for the default value.

+1
source share

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

0
source share

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

0
source share

All Articles