C# - Convert TimeStamp to DateTime [duplicate] - c#

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);
}

Related

how convert 1606813200000 to 2020-12-01T09:00:00.000Z with c# [duplicate]

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");

Convert string with date & UTC offset to DateTime [duplicate]

This question already has answers here:
Convert UTC/GMT time to local time
(12 answers)
Closed 6 years ago.
I am trying to parse a string "20160918000500 +0200" to DateTime containing offset value "+0200".
I tried the following but it gives invalid DateTime exception.
DateTime dtDateTime = DateTime.Parse("20160918000500 +0200",new CultureInfo("yyyyMMddHHmmss zzz"));
Is there a way to convert the String exactly to Datetime with UTC offset value?
To preserve your Offset, use the DateTimeOffset.ParseExact method:
string str = "20160918000500 +0200";
var result = DateTimeOffset.ParseExact(str, "yyyyMMddHHmmss zzz", CultureInfo.InvariantCulture);
Console.WriteLine(result);
I would suggest to try out one of ParseExact methods of DateTime class

Get date string from count of days since Epoch C# [duplicate]

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"));

How to generate a UTC Unix Timestamp in C# [duplicate]

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

Datetime format from UNIX epoch [duplicate]

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.

Categories