I need to encode current datetime into some unique string to store it in database. I found this article how to generate a unique token which expires after 24 hours? but for me generated token is to long (34 symbols)
Is there some other similar way to encode shorter string?
Perfect size <= 10 symbols.
Okay, if you want it from "about now" to some point in the future, and you want seconds granularity, and you want ASCII symbols, let's assume base64.
With 8 characters of base64, we can encode 6 bytes of data. That will give us 248 different values, which allows about 9 million years-worth of seconds. Given that range, we might as well use the DateTime.Ticks
property and divide by ticks-per-second, not worrying about the epoch. Full code coming later if you want it, but as a list of steps:
DateTime.UtcNow.Ticks
TimeSpan.TicksPerSecond
byte[]
, e.g. with BitConverter.GetBytes(long)
byte[]
as base64 using Convert.ToBase64String
See more on this question at Stackoverflow