Function that creates a timestamp in c#

274,252

Solution 1

I always use something like the following:

public static String GetTimestamp(this DateTime value)
{
    return value.ToString("yyyyMMddHHmmssfff");
}

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

Solution 2

I believe you can create a unix style datestamp accurate to a second using the following

//Find unix timestamp (seconds since 01/01/1970)
long ticks = DateTime.UtcNow.Ticks - DateTime.Parse("01/01/1970 00:00:00").Ticks;
ticks /= 10000000; //Convert windows ticks to seconds
timestamp = ticks.ToString();

Adjusting the denominator allows you to choose your level of precision

Solution 3

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 ;)

Solution 4

You can also use

Stopwatch.GetTimestamp().ToString();

Solution 5

when you need in a timestamp in seconds, you can use the following:

var timestamp = (int)(DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalSeconds;
Share:
274,252

Related videos on Youtube

Konstantinos
Author by

Konstantinos

Software Developer ASP.NET ASP.NET MVC C# Compact Framework WCF Services Rails ....

Updated on September 20, 2021

Comments

  • Konstantinos
    Konstantinos almost 3 years

    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.

  • Jon Skeet
    Jon Skeet about 15 years
    When you say "always increasing" - there's no guarantee that two calls to DateTime.UtcNow.Ticks will give different values though, is there? In other words you still need some caution before you use it as a unique timestamp.
  • Konstantinos
    Konstantinos about 15 years
    actually i just used it and i have duplicate values at some points.
  • Jennifer Miles
    Jennifer Miles about 15 years
    @Jon: with always increasing I meant: it's not a tickcount which flips every 48 days or so, it's a real tick count which increases over time, so a higher value is always a later timestamp.
  • Jennifer Miles
    Jennifer Miles about 15 years
    @Konstantinos: you can't avoid duplicates if you use timestamps. Timestamps aren't used for unique keys, but for marking a time. You said you needed millisecond precision, and ticks has 100ns precision. The thing is that you will have duplicates. If you don't want that you need a unique sequence in the DB, which is unfortunately not db agnostic.
  • PaulB
    PaulB about 15 years
    How come you get 21 months and only get 12? :)
  • Joe
    Joe about 15 years
    "it's a real tick count which increases over time". But the system clock can go backwards - e.g. at the end of daylight saving time if you are using local time, or because the system clock was adjusted.
  • Jennifer Miles
    Jennifer Miles about 15 years
    @Joe: true. Though isn't any other timestamp which isn't based on a central sequence but on the actual time suffering from that same drawback?
  • Joe
    Joe about 15 years
    @Frans: absolutely. I only made the comment because people should think about the implications of the solution they choose. For example I'd use UTC rather than local time to at least eliminate the problems of DST.
  • Konstantinos
    Konstantinos about 15 years
    @Frans Bouma The thing is that it gave me duplicated for a sequential operation that had at least 0.3 second + difference between it. I don't know if this is a compact framework peculiarity but it's strange since you say that it has a 100ns precision to give me duplicate values for sequential operations that are not that fast between the timestamp recordings...
  • Don Cote
    Don Cote over 14 years
    The token for year should be lowercase here: return value.ToString("yyyyMMddHHmmssffff");
  • Moshe Katz
    Moshe Katz about 12 years
    @Frans: I know this is old but... 100ns precision is not the same as 100ns accuracy. It may give you values with enough digits to see the 100ns space but the Windows system clock is usually only accurate to somewhere between 1 and 15 milliseconds. In other words, nanosecond-precision is a little useless. However, Konstantinos' time with 0.3 second interval is a little weird (because it is far more than 15ms).
  • Evgeniy Berezovsky
    Evgeniy Berezovsky over 9 years
    @RobV The question asks for millisecond precision, so you need 3 'f's at the end. Your 4 'f's give 100 microsend precision.
  • ctacke
    ctacke over 9 years
    Not weird at all since DateTime.Now in the Compact Framework is tied to the OS real time clock, which on most hardware only has a 1-second resolution. Device OEMs can make it high resolution, but very few choose to.
  • Redwolf
    Redwolf about 7 years
    Be aware, that Compact Framework does not transfer the milliseconds. They will always be 0. You have to mod the code and add something like this: int tick = Environment.TickCount % 1000; int ms = (tick >= MOffset) ? (tick - MOffset) : (1000 - (MOffset - tick)); ms = Math.Min(999, Math.Max(0, ms));