1

My sysadmin has failed to actually expire passwords in AD and now my password expiry warning system is starting to count back up instead of showing negative days. I'm expecting remainingDays to be a negative number for my test account ( 5 days pseudo-expired) from the following code, hoping someone can show me why I'm losing the negative. From what I've read on MSDN, DateTime.Subtract can return negative values.

DateTime today = DateTime.Now;
foreach (User user in users)
{
  DateTime expiryDate = user.pwdLastReset.AddDays(180);   //pwd expires every 180 days
  int remainingDays = Int32.Parse(expiryDate.Subtract(today).ToString("%d"));
  //snipped code to send warnings at different days remaining.
}
Flat Eric
  • 7,971
  • 9
  • 36
  • 45
dusk
  • 1,243
  • 1
  • 9
  • 10
  • 1
    http://stackoverflow.com/questions/1607336/calculate-difference-between-two-dates-number-of-days – Paul Abbott Jul 25 '16 at 17:38
  • Thanks for that! I had searched everywhere, but apparently was using the wrong search terms. Surprised that I had been doing it wrong all along and yet the results were seemingly correct until we started moving into the negative zone. – dusk Jul 25 '16 at 17:47

1 Answers1

1

DateTime.ToString("%d") will actually return the day of the month represented. For example, the following code:

DateTime prev = DateTime.Now.Subtract(TimeSpan.FromDays(10));
Console.WriteLine(prev.ToString("%d"));

prints out "15" (as in, "July 15, 2016"), not 10 or -10.