I was trying to save some stuff to the Log table with timestamp so I first did this:
public static string TimeStamp(
this DateTime datetime, string timestamptFormat = "yyyyMMddHHmmssffff")
{
return datetime.ToString(timestamptFormat);
}
And then I found a snippet like this:
static public string ToReverseTimestamp(this DateTime dateTime)
{
return string.Format("{0:10}", DateTime.MaxValue.Ticks - dateTime.Ticks);
}
I started wondering what the heck is reverse timestamp is useful for, and came across this article
Now my question is: if the second snippet is even correct? And how do you convert it back to “normal” timestamp or how do you get readable datetime information from it?
Make sure that the
DateTimeis converted to universal time before conversion to avoid time-zone problems:You can convert the value back to a
DateTimevalue by parsing thestringto along, calculatingMaxValue - (MaxValue - x) = xand constructing a newDateTimewithDateTimeKind.Utcfromx:Example: