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

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

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

C# - Convert TimeStamp to DateTime [duplicate]

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

C# - UTC automatically convert to 12h system [duplicate]

This question already has answers here:
DateTime.Value.ToString(format) gives me 12 hour clock
(4 answers)
Closed 6 years ago.
I'm working with a project and one of its functional requirement is to create a datetime range in which I will select a set of data based on that.
This range should take today's datetime starting from 8:00:00 to 18:00:00, and then I want to convert the format to be yyyy-MM-ddThh:mm:ssZ, so I'm doing the following to approach that:
DateTime fromTime = DateTime.Now;
TimeSpan ts = new TimeSpan(8,0,0);
fromTime = fromTime.Date + ts;
string fromTimeFormat = fromTime.ToString("yyyy-MM-ddThh:mm:ssZ");
DateTime toTime = DateTime.Now;
TimeSpan tss = new TimeSpan(18, 0, 0);
toTime = toTime.Date + tss;
string toTimeFormat = toTime.ToString("yyyy-MM-ddThh:mm:ssZ");
The problem is that, the toTimeFormat is being converted to the 12h system, so when I use it later it's being considered as 6:00 AM.
Any ideas please?
Because you are using hh specifier which is for 12-hour clock format.
You need to use HH specifier which is for 24-hour clock format.
string toTimeFormat = toTime.ToString("yyyy-MM-ddTHH:mm:ssZ");
And you can simplify your code as;
string fromTimeFormat = DateTime.Today.AddHours(6).ToString("yyyy-MM-ddThh:mm:ssZ");
string toTimeFormat = DateTime.Today.AddHours(18).ToString("yyyy-MM-ddTHH:mm:ssZ");

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