2013-01-10 116 views
6

我有這樣的任務來填充此字段:如何創建秒時間戳

x_fp_timestamp is the timestamp created when the form is generated. It is equal to the number of seconds since January 1, 1970 in UTC (Coordinated Universal Time).

所以我在C#中做的是

long ts = DateTime.Now.Ticks/TimeSpan.TicksPerSecond; 

但在這種情況下,我得到這個錯誤:

  • x_fp_timestamp : x_fp_timestamp invalid. Not within 15 minutes of present time: Thu Jan 10 21:30:25 GMT 2013. Expected 1357853425 plus/minus 900, but received 63493442997.

所以我的問題是如何在幾秒鐘內生成當前時間戳?

+0

可能重複:http://stackoverflow.com/questions/3354893/how-can-i-convert-a-datetime-to-the-number-of-seconds-since -1970 –

+1

這裏是一個很好的鏈接,看看它有很多很棒的其他示例以及Peretz [將DateTime對象轉換爲unix時間戳編號](http://www.java2s.com/Code/CSharp/Date-Time /ConvertsaDateTimeobjectintoaunixtimestampnumber.htm) – MethodMan

回答

16

DateTime.Now.Ticksdoes not start at 1970;嘗試這樣的事情,而不是:

(DateTime.Now.ToUniversalTime() - new DateTime (1970, 1, 1)).TotalSeconds 
+1

+ 1瓶Lagavulin男士!!! –