This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert UNIX timestamp to DateTime and vice versa?
How can I create a unix timestamp in C#? (e.g. 2012-10-10 14:00:00 -> 1349877600)
private double ConvertToTimestamp(DateTime value)
{
//create Timespan by subtracting the value provided from
//the Unix Epoch
TimeSpan span = (value - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());
//return the total seconds (which is a UNIX timestamp)
return (double)span.TotalSeconds;
}
DateTime.UtcNow - new DateTime(2012,10,10,14,0,0)).TotalSeconds
Related
This question already has answers here:
How can I convert a Unix timestamp to DateTime and vice versa?
(21 answers)
Given a DateTime object, how do I get an ISO 8601 date in string format?
(18 answers)
Closed 1 year ago.
can not convert date The following object to datetime in c#
{"date":1606813200000 , "open":119000.0, "high":130900.0, "low":107500.0, "close":113300.0, "volume":36892044.0}
I do not understand the relationship between the two
Why 1606813200000 == 2020-12-01T09:00:00.000Z?
can you cee , ralationship to site https://jsoneditoronline.org/
The number you see is a Javascript timestamp (number of milliseconds elapsed since January 1, 1970 00:00:00 UTC). You can test it in any browser console:
Following this article, you can convert back to DateTime object:
public static DateTime ConvertFromUnixTimestamp(double timestamp)
{
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
return origin.AddSeconds(timestamp / 1000); // convert from milliseconds to seconds
}
From the comment, I realize there is this method DateTimeOffset.FromUnixTimeMilliseconds as well:
// Convert Timestampt to DateTimeOffset
var time = DateTimeOffset.FromUnixTimeMilliseconds(1606813200000);
// Convert back to ISO string
var isoString = time.ToString("o");
This question already has answers here:
C# DateTime to "YYYYMMDDHHMMSS" format
(18 answers)
How can I convert a Unix timestamp to DateTime and vice versa?
(21 answers)
Closed 4 years ago.
I'm getting time and date in timestamp format. For example, for date and time of 4/3/2018 3:06:03 AM, I'm getting timestamp value 43193.12920166. I want to convert it to yyyyMMddHHmmss.
Unix timestamp - is amount of seconds from 1/1/1970, so just calc it:
static DateTime ConvertFromUnixTimestamp(double timestamp)
{
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
return origin.AddSeconds(timestamp);
}
This question already has answers here:
How do you convert epoch time in C#?
(14 answers)
Closed 9 years ago.
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
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.
I guess you should try it as since it is with respect to GMT
Also from the site you mention it assumes that the value is in milliseconds and not the traditional unix timestamp as in seconds
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
epoch = epoch.AddMilliseconds(yourvalue);// your case results to 4/5/2013 8:48:34 AM
This question already has answers here:
How can I convert a Unix timestamp to DateTime and vice versa?
(21 answers)
How do you convert epoch time in C#?
(14 answers)
Closed 9 years ago.
I have an integer 15791 which represents count of days since epoch and equals 27.03.2013, how can do this convert in C#?
public void method1()
{
...
int days_since_epoch = 15791;
// how convert `days_since_epoch` to "27.03.2013"
}
Thanks!
Adds a number of days to your epoch.
For example:
var epoch = new DateTime(...); // Your epoch (01/01/0001 or whatever)
var yourDate = epoch.AddDays(days_since_epoch);
Assuming your Epoch date is in a DateTime just use
DateTime epoch = new DateTime(1970,1,1);
int days_since_epoch = 15791;
DateTime converted = epoch.AddDays(days_since_epoch);
Simply use the AddDays method, and once you got your final date, format it as usual in the ToString().
Perhaps:
TimeSpan ts = TimeSpan.FromDays(15791);
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Add(ts);
DEMO
var date = new DateTime(1970,1,1).AddDays(15791);
Console.WriteLine(date.ToString("dd.MM.yyyy"));
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to convert UNIX timestamp to DateTime and vice versa?
I use this code to get the format datetime of a given UNIX epoch
DateTime epoch=new DateTime(1970,1,1);
DateTime dt=new DateTime(1325506582751000+epoch.Ticks());
The output result is 1975/x/y
which is incorrect.
Is the epoch incorrectly defined ? How can I get the correct one from any given datetime data ?
try this
public DateTime FromUnixTime(long unixTime)
{
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return epoch.AddSeconds(unixTime);
}
or you can read this article.