Convert LDAP AccountExpires to DateTime in C#

Viewed 33546

I want to convert 18 digit string from LDAP AccountExpires to Normal Date Time Format.

129508380000000000 >> May 26 2011

I got the above conversion from using the following link.

http://www.chrisnowell.com/information_security_tools/date_converter/Windows_active_directory_date_converter.asp?pwdLastSet,%20accountExpires,%20lastLogonTimestamp,%20lastLogon,%20and%20badPasswordTime

I tried to convert by using DateTime.Parse or Convert.ToDateTime. But no success.

Anyone know how to convert it? Thanks very much.

8 Answers

For Ruby

def ldapTimeConverter(ldap_time)
  Time.at((ldap_time/10000000)-11644473600)
end

I'll provide a perfect answer to this question using which you will be able to convert and DateTime to active directory long int format and will be also able to do the vice versa of it.

Here is a solution to it:-

To get DateTime from AD

string tickstring = de.Properties["accountExpires"][0].ToString();
long Ticks = (long) tickstring;

DateTime ReferenceDate = new DateTime(1601, 1, 1, 0, 0, 0, DateTimeKind.Utc);
long ExpireDateTicks = Ticks + ReferenceDate.Ticks;
DateTime ExpireDate = new DateTime(ExpireDateTicks);

To convert DateTime to AD long integer format

DateTime ReferenceDate = new DateTime(1601, 1, 1, 0, 0, 0, DateTimeKind.Utc);
DateTime ExpireDate = new DateTime(Request.EndDate.Year, Request.EndDate.Month, Request.EndDate.Day, 0, 0, 0);

long Ticks = ExpireDate.Ticks - ReferenceDate.Ticks;
NewUser.accountExpires = Ticks.ToString(); 
Related