How to get the correct timestamp in C #

I would like to get a 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); 

exit:

 000101010000000000 

I wanted something like:

 20140112180244 

How am I wrong?

+69
c # timestamp
Jan 19 '14 at 17:03
source share
1 answer

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

 String timeStamp = GetTimestamp(new DateTime()); 

:

 String timeStamp = GetTimestamp(DateTime.Now); 
+100
Jan 19 '14 at 17:05
source share



All Articles