English 中文(简体)
C# 准时目标
原标题:C# convert UTC int to DateTime object

我不知道为什么如此复杂!

我有一个长篇的花.。 我需要将这一数字转换成<代码>Datetime,以查询我的数据库(SQL服务器)。

我不知道为什么,但我无法从基本角度的搜索中找到可行的答案。

(关于额外信贷,我需要将我的返回<代码>Datetime在日末重新插入一个统一合同。)

令人难堪的是,我们必须提出这样一个基本问题!

最佳回答

My guess is it s going to be either milliseconds or seconds since a particular epoch - quite possibly the Unix epoch of January 1st 1970, midnight UTC.

So the code would look something like:

private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0,
                                                          DateTimeKind.Utc);

public static DateTime FromMillisecondsSinceUnixEpoch(long milliseconds)
{
    return UnixEpoch.AddMilliseconds(milliseconds);
}

2. 对二者或不同时代的人作出明显改变:

An alternative approach is to create a TimeSpan of the seconds/milliseconds since the epoch, and then add it to the epoch:

private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0,
                                                          DateTimeKind.Utc);

public static DateTime FromMillisecondsSinceUnixEpoch(long milliseconds)
{
    return UnixEpoch + TimeSpan.FromMilliseconds(milliseconds);
}

我不知道它们之间有任何重大差别——尽管有<代码”这一事实。 AddMilliseconds take a double und a long/表示,对于非常大的数值而言,timeSpan办法可能更为可取。 我怀疑它会带来任何变化,尽管:

问题回答

你们是否有二手、双手或什么? 在将其转换成标准之后(一个标准是100纳米秒),例如通过<代码>长期标准 = DBDateNum*timeSpan.TicksPerMillisecond;,尝试:

DateTime theDate = new DateTime(ticks, DateTimeKind.Utc);

http://www.epochconverter.com/“rel=“nofollow noreferer” https://www.epochconverter.com/。

Unix epoch (or Unix time or POSIX time or Unix timestamp) 是1970年1月1日(UTC/GMT)以来的二次数,没有计算出第二位数。

嗣后,

var epoch = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;

稍后,

private string epoch2string(int epoch) {
return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(epoch).ToShortDateString(); }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...