Expiry date calculation

In C# I am using the following function to compute the number of days left (used to produce an actual expiry date).:

    private static int DaysLeft ( uint expiryDate )
    {
      uint unixTimestamp = (uint)(DateTime.UtcNow.Subtract (new DateTime(1970,1,1))).TotalSeconds;
      int daysLeft = (int)(expiryDate - unixTimestamp) / 86400;
      return daysLeft;
    }

But when I add DaysLeft() to Now() it does not give me the same date as what is displayed at the Cryptlex portal. Usually off by one day.

I purloined that code from a sample of using Cryptlex at your github but perhaps there is a more precise logic I need to use to make sure the dates align properly. For example, using DateTime.UtcNow may account for it, depending on the time of day and timezone where the calculation is happening locally.

Any suggestions welcome.

Bob@Apex

Hi Bob,

Try following:

    public static double DaysLeft(uint expiryDate)
	{
		var localExpiryDate = DateTimeOffset.FromUnixTimeSeconds(expiryDate).LocalDateTime;
		return (DateTime.Now - localExpiryDate).TotalDays;
	}