1

Here's what I tried so far:

var value = DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds.ToString()

It gives me the answer below but I would like to get a whole number:

"1500092088.9501"

Alan2
  • 23,493
  • 79
  • 256
  • 450
  • 1
    So... you're looking for instructions on how to [round to an integer](https://msdn.microsoft.com/en-us/library/wyk4d9cy(v=vs.110).aspx)? – BJ Myers Jul 15 '17 at 04:22
  • whats the output of this code? – Ali Jul 15 '17 at 04:22
  • 2
    @danyolgiax this is not a duplicate. the question you link is showing how to get a total second difference, Ops question already gets the difference and required rounding. – Joe_DM Jul 15 '17 at 04:29

3 Answers3

2

Below are a few examples of various ways you can deal with this sort of scenario

double secondsSince1970 = DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;

// int logic. ints are whole numbers so casting to an int will drop all decimals with no rounding.
Console.WriteLine(((long)secondsSince1970).ToString());

// Math logic. .Net gives us some handy Maths to work with rounding decimals
// Round up
Console.WriteLine(Math.Ceiling(secondsSince1970).ToString());
// Round down
Console.WriteLine(Math.Floor(secondsSince1970).ToString());
// Round nearest
Console.WriteLine(Math.Round(secondsSince1970).ToString());

EDIT: Changed the int logic to cast to a long instead of an int because the int.MaxValue will be reached near the start of the year 2038

Joe_DM
  • 985
  • 1
  • 5
  • 12
1

I would just use DateTimeOffset to get the seconds count:

long secsSinceEpoch = DateTimeOffset.UtcNow.ToUnixTimeSeconds();

This seems to be what you're looking for though, of course, you'd probably want to call ToString() on that if you want it as an actual string:

var secsSinceEpoch = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString();
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Would that also give numbers after the decimal? I would like to get a whole number. – Alan2 Jul 15 '17 at 04:32
  • 1
    No, because it's a long, not a double. – paxdiablo Jul 15 '17 at 04:33
  • A "whole" number does not have a decimal, it is an integer – Kevin Jul 15 '17 at 04:37
  • 1
    Your approach interested me so I did a quick benchmark and while this is the sort of operation that likely won't need optimization, I thought I'd share the results anyway just out of interest. Casting the datetime total seconds runs 10000000 loops in approximately 588 milliseconds while the DateTimeOffset ToUnixTimeSeconds runs 10000000 loops in 2111 milliseconds. So it's about 4x slower – Joe_DM Jul 15 '17 at 04:38
  • @Alan this answer does seem to address your requirements and it's less lines of code which could be good for your situation. Unless you're doing millions of iterations with this logic, the performance is totally fine. – Joe_DM Jul 15 '17 at 04:42
  • I guess one problem with this is I am running on WIndows and UtcNow does not seem to have .ToUnixTimeSeconds() – Alan2 Jul 15 '17 at 04:46
  • 1
    @Alan the `ToUnixTimeSeconds` method is available on the `DateTimeOffset` type, It won't exist directly on a `DateTime` – Joe_DM Jul 15 '17 at 04:56
1

I thought it worth mentioning - If you want the number of seconds from the start of the epoch time, which is 1/1/1970, You don't need to do subtraction... You just need to get the current datetime and do the conversion to seconds.

Joe_DM provides a good answer to your rounding problem.

(Would leave as comment, but still a little short on rep :) )

Andrew Cameron
  • 73
  • 1
  • 12