Wednesday, April 13, 2011

Displaying ActiveDirectory's "whenChanged" attribute in C#

"whenChanged" comes through without the time offset. Not a big deal to fix it, but a quick Google search yielded no results on how to deal with it. Here's my solution to properly display Eastern Daylight/Standard time:
TimeZone localZone = TimeZone.CurrentTimeZone;
DateTime generalizedDate = DateTime.Parse(result.Properties["whenChanged"][0].ToString());
double hourDelta = localZone.IsDaylightSavingTime(generalizedDate) ? -4D : -5D;
DateTime lastUpdated = generalizedDate.AddHours(hourDelta);
BTW, Sysinternals' AD Explorer is great for finding out meta information (i.e. data type, property name) about AD attributes.

1 comment:

Marc said...

Thank you for you post, that gives me hint and save me a lot of time!! :-)

here is my solution which can be using in any timezones:



DateTime utcTime = DateTime.Parse("30.10.2018 18:21:34")
DateTime localtime = ConvertUTCToLocalTime(utcTime);


public static DateTime ConvertUTCToLocalTime(DateTime UTCTime)
{
var localZone = TimeZone.CurrentTimeZone;
var offset = localZone.GetUtcOffset(UTCTime);
var localTime = UTCTime.AddHours(offset.Hours);
return localTime;
}