简体   繁体   中英

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM