SoFunction
Updated on 2025-03-06

Example analysis of basic usage of C# timestamps

This article describes the basic usage of C# timestamps. Share it for your reference. The details are as follows:

1. How to generate a timestamp in C#

/// <summary> 
/// Get the timestamp/// </summary> 
/// <returns></returns> 
public static string GetTimeStamp() 
{ 
  TimeSpan ts =  - new DateTime(1970, 1, 1, 0, 0, 0, 0); 
  return Convert.ToInt64().ToString(); 
} 

It is often found that many places use a timestamp to represent time. For example: 1370838759 represents June 10, 2013 at 12:32:39. We need a tool to easily convert this time format

2. What is a timestamp?

Timestamp, also known as Unix Stamp. The number of seconds elapsed from January 1, 1970 (midnight in UTC/GMT), regardless of leap seconds.

3. Convert C# timestamp to normal time

// Convert the timestamp to C# Format Timeprivate DateTime StampToDateTime(string timeStamp){
  DateTime dateTimeStart = (new DateTime(1970, 1, 1));
  long lTime = (timeStamp + "0000000");
  TimeSpan toNow = new TimeSpan(lTime);
  return (toNow);
}
// Convert DateTime time format to Unix timestamp formatprivate int DateTimeToStamp( time)
{
   startTime = (new (1970, 1, 1));
  return (int)(time - startTime).TotalSeconds;
}

I hope this article will be helpful to everyone's C# programming.