6

I have this task to populate this field:

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).

So what I do in C# is

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

But in that case I am getting this error:

  • 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.

So my question is how to generate current timestamp in seconds?

Michele Ceo
  • 23
  • 1
  • 8
NoWar
  • 34,755
  • 78
  • 302
  • 475
  • Possible duplicate: http://stackoverflow.com/questions/3354893/how-can-i-convert-a-datetime-to-the-number-of-seconds-since-1970 – Davin Tryon Jan 10 '13 at 21:59
  • 1
    here is a good link to look at it has so many great other examples as well Peretz [Converts a DateTime object into a unix timestamp number](http://www.java2s.com/Code/CSharp/Date-Time/ConvertsaDateTimeobjectintoaunixtimestampnumber.htm) – MethodMan Jan 10 '13 at 22:09

2 Answers2

17

DateTime.Now.Ticks does not start at 1970; try something like this instead:

 (DateTime.Now.ToUniversalTime() - new DateTime (1970, 1, 1)).TotalSeconds
praseodym
  • 2,124
  • 15
  • 29
1

I found DateTimeOffset.ToUnixTimeSeconds to work best for my specific situation (Unity 3D [.NET standard 2.1])

    long timeNow = DateTimeOffset.Now.ToUnixTimeSeconds();
    string timeStamp = timeNow.ToString();
Jacksonkr
  • 30,630
  • 38
  • 174
  • 276