Encode current date into short unique string

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.

Jon Skeet
people
quotationmark

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:

  • Take DateTime.UtcNow.Ticks
  • Divide by TimeSpan.TicksPerSecond
  • Convert the result into a byte[], e.g. with BitConverter.GetBytes(long)
  • Encode the least-significant 6 bits (I'm hopeless with endianness - either the first or last 6 bytes of the byte[] as base64 using Convert.ToBase64String

people

See more on this question at Stackoverflow