Ad accountexpires setting issue

This code in a console program works fine

DateTime enteredDate = DateTime.Parse("2/2/2019 1:16:47 PM");
//change the time to 6pm
enteredDate = enteredDate.Date.Add(new TimeSpan(18, 00, 0));
Debug.WriteLine($"Entered date : {enteredDate}");
Debug.WriteLine($"FromFileTime: DateTime.FromFileTime(enteredDate.ToFileTime())}");

output is

Entered date : 2/2/2019 6:00:00 PM
FromFileTime: 2/2/2019 6:00:00 PM

When i set this time for an AD attribute like this

directoryEntry.Properties["accountExpires"].Value = Convert.ToString(enteredDate .ToFileTime());

it is writing the time part as 12:00:00 PM instead of 6pm. What am I doing wrong ?

1 answer

  • answered 2019-02-07 00:22 Gabriel Luci

    I assume this is a time zone issue and you are in a UTC+6 time zone.

    I don't think you have a problem at all. The accountExpires attribute stores the time as a number that "represents the number of 100-nanosecond intervals since January 1, 1601 (UTC)". That's exactly what ToFileTime() does for you. You're doing it right.

    But note that it is stored in UTC. If you are seeing it as 12:00 PM, that's probably because you are reading it as UTC time.

    6:00 PM your time is 12:00 PM UTC