I have ExpiresOn
for Azure AD token which is a type of DateTimeOffset
. My model class is below,
public class AadToken
{
public string Value { get; set; }
public DateTimeOffset ExpiresIn { get; set; }
}
and a method returns the above model like below,
return new AadToken { ExpiresIn = accessToken.ExpiresOn, Value = accessToken.Token };
Now I need to convert it into seconds and set like below,
//tokenModel.ExpiresIn needs to convert in sec.
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(tokenModel.ExpiresIn);
How can I do this?
That's like asking how many seconds there are in 2021-08-27 20:10:03
. A date is an instant in time, not a duration, so the question has no answer.
You can calculate how many seconds are between two dates, eg now and the target date, eg tokenModel.ExpiresIn - DateTimeOffset.Now
. This returns a TimeSpan class with the duration. You can use the TotalSeconds property to get the duration in seconds
var expSpan=(tokenModel.ExpiresIn - DateTimeOffset.Now);
entry.AbsoluteExpirationRelativeToNow = expSpan.TotalSeconds;