将utc时间和偏移量转换为DateTime [英] convert utc time and offset to DateTime

查看:105
本文介绍了将utc时间和偏移量转换为DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个保存在数据库中的日期时间(以utc表示),我也知道utc偏移量的格式如下。

i have a datetime(in utc) saved in database and i also know the utc offset in the following format.

-03:00:00

如何将其转换为 DateTime

推荐答案

这是将偏移应用于 DateTime 已经是创建一个 TimeSpan 结构,其中包含您的偏移量值,然后只需将偏移量添加到原始 DateTime 值即可。

This simplest way to apply an "offset" to a DateTime that you already have is to create a TimeSpan structure which holds your offset value, and then simply "add" the offset to the original DateTime value.

例如:

DateTime utcDateTime = DateTime.Parse("29 July 2010 14:13:45");
TimeSpan offSet = TimeSpan.Parse("-03:00:00");
DateTime newDateTime = utcDateTime + offSet;
Console.WriteLine(newDateTime);

这将产生以下输出:

29/07/2010 11:13:45

这是原始时间(2010年7月29日14: 13:45)减去3小时(偏移量--03:00:00)。

which is the original time (29 July 2010 14:13:45) minus 3 hours (the offset - -03:00:00).

请注意,此技术只是对 DateTime 的值,并且不考虑任何时区。

Note that this technique is merely performing simple arithmetic with your DateTime value and does not take any time zones into account.

这篇关于将utc时间和偏移量转换为DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆