I was wondering, is there a way to create a timestamp in c# from a datetime? I need a millisecond precision value that also works in Compact Framework(saying that since DateTime.ToBinary() does not exist in CF).
My problem is that i want to store this value in a database agnostic way so i can sortby it later and find out which value is greater from another etc.
You could use the DateTime.Ticks property, which is a long and universal storable, always increasing and usable on the compact framework as well. Just make sure your code isn't used after December 31st 9999 ;)
You can also use
Stopwatch.GetTimestamp().ToString();
when u need in seconds:
If you want timestamps that correspond to actual real times BUT also want them to be unique (for a given application instance), you can use the following code:
I always use something like the following:
This will give you a string like 200905211035131468, as the string goes from highest order bits of the timestamp to lowest order simple string sorting in your SQL queries can be used to order by date if you're sticking values in a database
I believe you can create a unix style datestamp accurate to a second using the following
Adjusting the denominator allows you to choose your level of precision