用 C# 计算 与 java 一致的unix时间戳 (长时间整形 如:1476956079961)

时间:2022-04-28 02:38:28

背景:

调用java提供接口,需要长时间整形作为验证。

 

预备知识:

1、 java 的 System.currentTimeMillis() 计算的长整型,是从1970年1月1日开始,截止当前的毫秒数。

 

2、C#中计算毫秒数的方法 TimeSpan.TotalMilliseconds

 

3、我天朝是东八区,时区也要计算在内。所以起始期间应为1970年1月1日8:00

 

代码:

        /// <summary>
        /// 计算当前长时间整形  
        /// </summary>
        private long GetTimeLong()
        {            
            var javaLongTime = new DateTime(1970, 1, 1, 8, 0, 0);   
            var now = DateTime.Now;
            var interval = now - javaLongTime;  
            return (long)interval.TotalMilliseconds;   

        }    

 

扩展:

TimeSpan 除了毫秒总数,其他时间单位:

// Define two dates. DateTime date1 = new DateTime(2010, 1, 1, 8, 0, 15); DateTime date2 = new DateTime(2010, 8, 18, 13, 30, 30); // Calculate the interval between the two dates. TimeSpan interval = date2 - date1; Console.WriteLine("{0} - {1} = {2}", date2, date1, interval.ToString()); // Display individual properties of the resulting TimeSpan object. Console.WriteLine(" {0,-35} {1,20}", "Value of Days Component:", interval.Days); Console.WriteLine(" {0,-35} {1,20}", "Total Number of Days:", interval.TotalDays); Console.WriteLine(" {0,-35} {1,20}", "Value of Hours Component:", interval.Hours); Console.WriteLine(" {0,-35} {1,20}", "Total Number of Hours:", interval.TotalHours); Console.WriteLine(" {0,-35} {1,20}", "Value of Minutes Component:", interval.Minutes); Console.WriteLine(" {0,-35} {1,20}", "Total Number of Minutes:", interval.TotalMinutes); Console.WriteLine(" {0,-35} {1,20:N0}", "Value of Seconds Component:", interval.Seconds); Console.WriteLine(" {0,-35} {1,20:N0}", "Total Number of Seconds:", interval.TotalSeconds); Console.WriteLine(" {0,-35} {1,20:N0}", "Value of Milliseconds Component:", interval.Milliseconds); Console.WriteLine(" {0,-35} {1,20:N0}", "Total Number of Milliseconds:", interval.TotalMilliseconds); Console.WriteLine(" {0,-35} {1,20:N0}", "Ticks:", interval.Ticks); // the example displays the following output: // 8/18/2010 1:30:30 PM - 1/1/2010 8:00:15 AM = 229.05:30:15 // Value of Days Component: 229 // Total Number of Days: 229.229340277778 // Value of Hours Component: 5 // Total Number of Hours: 5501.50416666667 // Value of Minutes Component: 30 // Total Number of Minutes: 330090.25 // Value of Seconds Component: 15 // Total Number of Seconds: 19,805,415 // Value of Milliseconds Component: 0 // Total Number of Milliseconds: 19,805,415,000 // Ticks: 198,054,150,000,000

 

参见:https://msdn.microsoft.com/en-us/library/system.timespan(v=vs.110).aspx