This is question is not a duplicate, this quesitons demonstrates a problem with a method of conversion, not how to perform the conversion. Read the question in full.
I have a timestamp which I believe is a unix time stamp, when using the following converter it correctly converts the stamp
Value: 1365151714493
http://www.epochconverter.com/
I have looked around and found an example on how to convert this to a datetime obect and the method seems simple, create a datetime object and set the date to the might night on 1/1/1970 and add the value as second:
public static DateTime? ConvertUnixTimeStamp(string unixTimeStamp)
{
return new DateTime(1970, 1, 1, 0, 0).AddSeconds(Convert.ToDouble(unixTimeStamp));
}
The problem is everytime I call this mehod with the value above I get a value out of range exception.
Do I need to do anything with the value first? the string converts to a double ok. the exception is thrown when calling the AddSeconds(double)
methos
Convert from epoch to human-readable dateString date = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new java.util.Date (epoch*1000)); Epoch in seconds, remove '*1000' for milliseconds. myString := DateTimeToStr(UnixToDateTime(Epoch)); Where Epoch is a signed integer. Replace 1526357743 with epoch.
use strict; use warnings; use Time::Piece; my $datestring = '07-06-2019 21:13:00'; my $time = localtime->strptime($datestring, '%d-%m-%Y %H:%M:%S'); my $epoch = $time->epoch; ... my $time = localtime($epoch); my $datestring = $time->strftime('%d-%m-%Y %H:%M:%S');
If you have a list of timestamp needed to convert to date, you can do as below steps: 1. In a blank cell next to your timestamp list and type this formula =(((A1/60)/60)/24)+DATE(1970,1,1), press Enter key, then drag the auto fill handle to a range you need.
That timestamp (1365151714493) is in milliseconds, not seconds. You'll need to divide by 1000
or use AddMilliseconds
instead. If it's treated as seconds, it's a date some 43,259 (rough calculation) years in the future. This is beyond the range of DateTime
which maxes out at the year 10000, thus throwing the ArgumentOutOfRangeException
.
public static DateTime? ConvertUnixTimeStamp(string unixTimeStamp)
{
return new DateTime(1970, 1, 1, 0, 0, 0).AddMilliseconds(Convert.ToDouble(unixTimeStamp));
}
You may also want to consider forcing it to GMT as V4Vendetta suggested. In addition, if you expect to have a mix of formats (seconds OR milliseconds) perhaps a quick size check on the parsed value might be prudent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With