229

I would like to get valid timestamp in my application so I wrote:

public static String GetTimestamp(DateTime value)
{
    return value.ToString("yyyyMMddHHmmssffff");
}
//  ...later on in the code
String timeStamp = GetTimestamp(new DateTime());
Console.WriteLine(timeStamp);

output:

000101010000000000

I wanted something like:

20140112180244

What have I done wrong?

CarenRose
  • 1,266
  • 1
  • 12
  • 24
Wojciech Ketrzynski
  • 2,503
  • 2
  • 15
  • 12

6 Answers6

258

Your mistake is using new DateTime(), which returns January 1, 0001 at 00:00:00.000 instead of current date and time. The correct syntax to get current date and time is DateTime.Now, so change this:

String timeStamp = GetTimestamp(new DateTime());

to this:

String timeStamp = GetTimestamp(DateTime.Now);
ekad
  • 14,436
  • 26
  • 44
  • 46
  • What is the other way around? from timestamp to datetime – DanielV Jun 15 '17 at 17:55
  • 3
    @DanielV see here: [Converting a String to DateTime](https://stackoverflow.com/q/919244/1905949). – ekad Jun 15 '17 at 22:26
  • 3
    `double timestamp = 1498122000; DateTime fecha = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc).AddSeconds(timestamp);` extracted from [here](https://stackoverflow.com/a/250400/1257607) – DanielV Jun 18 '17 at 09:36
103
var Timestamp = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();
Mohamed.Abdo
  • 2,054
  • 1
  • 19
  • 12
41
var timestamp = DateTime.Now.ToFileTime();

//output: 132260149842749745

This is an alternative way to individuate distinct transactions. It's not unix time, but windows filetime.

From the docs:

A Windows file time is a 64-bit value that represents the number of 100- 
nanosecond intervals that have elapsed since 12:00 midnight, January 1, 1601 
A.D. (C.E.) Coordinated Universal Time (UTC).
Jeff
  • 810
  • 8
  • 18
  • 5
    It would help if you added some explanation as to why your code answer is better than the others so other users will understand why to use you solution to this problem. Code only answers are not so helpful without an explanation. – Brian Tompsett - 汤莱恩 Jun 28 '18 at 22:20
  • 5
    Helps me to get easily a timestamp style to invalidate cache. +1 – Adavo Dec 19 '18 at 09:41
  • 1
    Great answer! This was a stitch in time saving nine on something I was struggling with. I owe you much gratitude! – Joseph Feb 25 '21 at 17:52
10

For UTC:

string unixTimestamp = Convert.ToString((int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds);

For local system:

string unixTimestamp = Convert.ToString((int)DateTime.Now.Subtract(new DateTime(1970, 1, 1)).TotalSeconds);
Yousha Aleayoub
  • 4,532
  • 4
  • 53
  • 64
5
Int32 unixTimestamp = (Int32)(TIME.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;

"TIME" is the DateTime object that you would like to get the unix timestamp for.

shahar
  • 355
  • 2
  • 18
sachin
  • 202
  • 3
  • 2
3
internal static string UnixToDate(int Timestamp, string ConvertFormat)
{
    DateTime ConvertedUnixTime = DateTimeOffset.FromUnixTimeSeconds(Timestamp).DateTime;
    return ConvertedUnixTime.ToString(ConvertFormat);
}

int Timestamp = (int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;

Usage:

UnixToDate(1607013172, "HH:mm:ss"); // Output 16:32:52
Timestamp; // Output 1607013172
Marco Concas
  • 1,665
  • 20
  • 25