u_date = 1688066111
DateTime p_date = DateTimeOffset.FromUnixTimeSeconds(u_date).DateTime;
Gives: 2023-06-29 19:15:11
long unixTime = ((DateTimeOffset)p_date).ToUnixTimeSeconds();
Gives: 1688058911 (2023-06-29 17:15:11)
How to fix that problem?
Doing it step by step will reveal what happens:
DateTimeOffset.FromUnixTimeSeconds(u_date)will give you2023-06-29 19:15:11 +00:00, i.e. a date/time in UTC. ReferenceGetting the property
DateTimewill erase the time zone information (the resulting p_date hasDateTimeKind.Unspecified). Reference((DateTimeOffset)p_date)will interpret p_date as local time, i.e. in the current timezone which seems to be +02:00 in your case. ReferenceYou can fix this problem by avoiding the round-trip via
DateTime, using onlyDateTimeOffset. Or - as Jon noted in his comment - by using the propertyUtcDateTimein step 2 (instead ofDateTime) which will produce a a DateTime object withDateTimeKind.Utc(instead ofDateTimeKind.Unspecified).