0

I am working on a C# project where I have a date/time in the format of 2012-11-24 15:35:18 and I need to convert this into an epoch time stamp.

Everything I've found on Google is to convert an epoch time stamp into a human readable but I need it to be done the other way round.

Thanks for any help you can provide.

Boardy
  • 35,417
  • 104
  • 256
  • 447

3 Answers3

2

I found this here:

epoch = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;

Instead of DateTime.Now, you should be able to input your desired time.

David Brunow
  • 1,299
  • 1
  • 12
  • 13
  • Thanks, I looked on that site, didn't notice all of the different code examples at the bottom – Boardy Nov 25 '12 at 01:31
1

You didn't say your exact use case, but the standard .NET DateTime has a Ticks attribute which is defined as:

The value of this property represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001, which represents DateTime.MinValue. It does not include the number of ticks that are attributable to leap seconds.

This is essentially an epoch based time, if it will suit your needs. Otherwise, with this value, you should be easily able to compute a conversion to another epoch time keeping method.

SAJ14SAJ
  • 1,698
  • 1
  • 13
  • 32
0

You need to use TryParse:

string input = "2012-11-24 15:35:18";
DateTime dateTime;
if (DateTime.TryParse(input, out dateTime))
{
    ulong epoch = (dateTime.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
}
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49