SoFunction
Updated on 2025-03-01

C# Example explanation of getting the current total milliseconds

Under .Net, a long-type time integer is obtained, which specifically represents the number of 100 nanoseconds of time elapsed since midnight on January 1, 0001. Convert to seconds to Ticks/10000000, and to milliseconds Ticks/10000.

If you want to get the number of milliseconds elapsed from January 1, 1970 to the current time, the code is as follows:

//Get the current Tickslong currentTicks= DateTime .;
DateTime dtFrom = new DateTime (1970, 1, 1, 0, 0, 0, 0);
long currentMillis = (currentTicks - ) / 10000;

Similar to Java:()

Convert unit:

1 second = 1000 milliseconds

1 millisecond = 1000 subtle

1 microsecond = 1000 nanoseconds

Supplement: Several ways to convert timestamp byte[] to datetime

Recommended method:

DateTime now = ;
byte[] bts = (());
DateTime rt = (BitConverter.ToInt64(bts, 0)); 

I used 2 bytes, the date range is 2000-01-01 ~ 2127-12-31, and the following is the conversion method:

 // Date -> byte[2] 
 public static byte[] DateToByte(DateTime date) 
 { 
  int year =  - 2000; 
  if (year < 0 || year > 127) 
  return new byte[4]; 
  int month = ; 
  int day = ; 
  int date10 = year * 512 + month * 32 + day; 
  return ((ushort)date10); 
 } 
 // byte[2] -> Date 
 public static DateTime ByteToDate(byte[] b) 
 { 
  int date10 = (int)BitConverter.ToUInt16(b, 0); 
  int year = date10 / 512 + 2000; 
  int month = date10 % 512 / 32; 
  int day = date10 % 512 % 32; 
  return new DateTime(year, month, day); 
 } 

Call example:

byte[] write = DateToByte(); 
(ByteToDate(write).ToString("yyyy-MM-dd"));
/// <summary> 2. /// Convert BYTE array to DATETIME type 3. /// </summary> 4. /// <param name="bytes"></param> 5. /// <returns></returns> 6. private DateTime BytesToDateTime(byte[] bytes) {
  if (bytes != null &amp;&amp;  &gt;= 5)
  {
  int year = 2000 + Convert.ToInt32((new byte[1] { bytes[0] }, 0));
  int month = Convert.ToInt32((new byte[1] { bytes[1] }, 0));
  int date = Convert.ToInt32((new byte[1] { bytes[2] }, 0));
  int hour = Convert.ToInt32((new byte[1] { bytes[3] }, 0));
  int minute = Convert.ToInt32((new byte[1] { bytes[4] }, 0));
  DateTime dt = new DateTime(year, month, date, hour, minute, 0);
  return dt;
  }
  else19.  {
  return new DateTime();
  }
 }

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.