C#转换UTC INT到DateTime对象

编程入门 行业动态 更新时间:2024-10-11 13:24:59
本文介绍了C#转换UTC INT到DateTime对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我不知道为什么会这么复杂!

I don't know why this is so complicated!

我有一个插件,它是传递一个长整型UTC。我需要这个数字转换成的DateTime 来查询我的数据库(SQL Server)。

I have a plugin that is passing in a long int UTC. I need to convert that number into a DateTime to query my database (SQL Server).

我不知道为什么,但我找不到从基本的谷歌搜索一个可行的答案。

I don't know why, but I can't find a workable answer from a basic google search.

(对于额外的学分,我需要把我的返回的DateTime 返回到一个UTC在这一天结束。)

(For extra credit, I need to turn my returned DateTime back into a UTC at the end of the day.)

这是尴尬不得不问这样一个基本问题! :)

This is embarrassing to have to ask such a basic question! :)

推荐答案

我的猜测是,这将是无论是毫秒或秒,因为一个特殊的时期 - 1970年1月1日,午夜(UTC)的很可能是Unix纪元

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.

所以,code看起来是这样的:

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

请为秒的明显变化,或从不同的时期:)

Make the obvious changes for seconds, or from a different epoch :)

这是另一种方法是创建一个时间跨度自纪元秒/毫秒,然后将其添加到时代:

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 需要双而不是的长表明,对于非常大的数值,在时间跨度办法可能是preferable。我怀疑它会让任何区别,但:)

I don't know of any significant difference between them - although the fact that AddMilliseconds takes a double instead of a long suggests that for very large values, the TimeSpan approach may be preferable. I doubt that it'll make any difference though :)

更多推荐

C#转换UTC INT到DateTime对象

本文发布于:2023-11-09 03:50:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1571320.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   UTC   INT   DateTime

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!