C#中 DateTime , DateTime2 ,DateTimeOffset 之间的小区别 (转载)
SQL Server中DateTime , DateTime2 ,DateTimeOffset的区别
闲来无事列了个表比对一下这3兄弟之间还是有一点差距的╮(╯_╰)╭
|
|
DateTime
|
DateTime2
|
DateTimeOffset
|
| 日期范围 |
1753-01-01到 9999-12-31
|
0001-01-01 到 9999-12-31
|
0001-01-01 到 9999-12-31
|
| 时间范围 |
00:00:00 到 23:59:59.997
|
00:00:00 到 23:59:59.9999999
|
00:00:00 到 23:59:59.9999999
|
|
存储字节大小
|
8字节
|
精度小于 3 时为 6 个字节;精度为 3 和 4 时为 7 个字节。 所有其他精度则需要 8 个字节
|
精度小于 3 时为 6 个字节;精度为 3 和 4 时为 7 个字节。 所有其他精度则需要 8 个字节
|
| 精度 |
舍入到 .000、.003 或 .007 秒三个增量。
|
100 纳秒
|
100 纳秒
|
|
支持自定义小数精度
|
否
|
是 |
是
|
|
时区
|
无
|
无
|
-14:59 到 +14:59
|
至于 DateTimeOffset ,时间部分都是UTC时间。
比方说现在我们在GMT+8:00 的位置,所以当地时间是 2019-12-09 21:33:26,如果用DateTimeOffset 来表示就是 2019-12-09 13:33:26 -08:00
DECLARE @dto DATETIMEOFFSET(7)=N'2019-12-09 21:33:26 +08:00'
DECLARE @dtLocal DATETIME=N'2019-12-09 21:33:26'
DECLARE @dtUTC DATETIME=N'2019-12-09 13:33:26' SELECT @dto,@dtLocal,DATEDIFF(HH,@dto,@dtLocal),@dtUTC,DATEDIFF(HH,@dto,@dtUTC)
结果如下:

然后就是在SQL Server中,当DateTimeOffset 转格式成为其它2兄弟的时候,时区会被舍去的啊……的啊……的啊
DECLARE @dto DATETIMEOFFSET(7)=SYSDATETIMEOFFSET()
SELECT @dto,CAST(@dto AS DATETIME),CAST(@dto AS DATETIME2(7))
结果如下:

C#中DateTime和DateTimeOffset的区别
可以理解为DateTimeOffset是带时区偏差的DateTime,如下MSDN代码所示:
using System;
using System.Collections.ObjectModel; public class TimeOffsets
{
public static void Main()
{
DateTime thisDate = new DateTime(, , , , , );
DateTime dstDate = new DateTime(, , , , , );
DateTimeOffset thisTime; thisTime = new DateTimeOffset(dstDate, new TimeSpan(-, , ));
ShowPossibleTimeZones(thisTime); thisTime = new DateTimeOffset(thisDate, new TimeSpan(-, , ));
ShowPossibleTimeZones(thisTime); thisTime = new DateTimeOffset(thisDate, new TimeSpan(+, , ));
ShowPossibleTimeZones(thisTime);
} private static void ShowPossibleTimeZones(DateTimeOffset offsetTime)
{
TimeSpan offset = offsetTime.Offset;
ReadOnlyCollection<TimeZoneInfo> timeZones; Console.WriteLine("{0} could belong to the following time zones:",
offsetTime.ToString());
// Get all time zones defined on local system
timeZones = TimeZoneInfo.GetSystemTimeZones();
// Iterate time zones
foreach (TimeZoneInfo timeZone in timeZones)
{
// Compare offset with offset for that date in that time zone
if (timeZone.GetUtcOffset(offsetTime.DateTime).Equals(offset))
Console.WriteLine(" {0}", timeZone.DisplayName);
}
Console.WriteLine();
}
}
// This example displays the following output to the console:
// 6/10/2007 12:00:00 AM -07:00 could belong to the following time zones:
// (GMT-07:00) Arizona
// (GMT-08:00) Pacific Time (US & Canada)
// (GMT-08:00) Tijuana, Baja California
//
// 3/10/2007 12:00:00 AM -06:00 could belong to the following time zones:
// (GMT-06:00) Central America
// (GMT-06:00) Central Time (US & Canada)
// (GMT-06:00) Guadalajara, Mexico City, Monterrey - New
// (GMT-06:00) Guadalajara, Mexico City, Monterrey - Old
// (GMT-06:00) Saskatchewan
//
// 3/10/2007 12:00:00 AM +01:00 could belong to the following time zones:
// (GMT+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna
// (GMT+01:00) Belgrade, Bratislava, Budapest, Ljubljana, Prague
// (GMT+01:00) Brussels, Copenhagen, Madrid, Paris
// (GMT+01:00) Sarajevo, Skopje, Warsaw, Zagreb
// (GMT+01:00) West Central Africa
微软官方对C#中DateTime和DateTimeOffset区别的解释:
Choosing between DateTime, DateTimeOffset, TimeSpan, and TimeZoneInfo
C#中 DateTime , DateTime2 ,DateTimeOffset 之间的小区别 (转载)的更多相关文章
- DateTime , DateTime2 ,DateTimeOffset 之间的小区别
闲来无事列了个表比对一下这3兄弟之间还是有一点差距的╮(╯_╰)╭ DateTime DateTime2 DateTimeOffset 日期范围 1753-01-01到 9999-12-31 00 ...
- mysql中DATETIME,DATE和TIMESTAMP的区别整理
简而言之.看格式,DATE 是 年月日YYYY-MM-DD,DATETIME 是 年月日时分秒YYYY-MM-DD HH:MM:SS,TIMESTAMP是 年月日时分秒YYYY-MM-DD HH:MM ...
- Python中list的删除del&remove小区别
del删除时候指定下标,remove必须指定具体的值
- php中mysql_fetch_row() 和mysql_fetch_array之间有什么区别
mysql_fetch_row是从结果集取出1行数组,作为枚举 mysql_fetch_array是从结果集取出一行数组作为关联数组,或数字数组,两者兼得eg:$sql="select ab ...
- Java中print、printf、println的区别(转载)
printf主要是继承了C语言的printf的一些特性,可以进行格式化输出 print就是一般的标准输出,但是不换行 println和print基本没什么差别,就是最后会换行 System.out.p ...
- JavaScript中var、let和const的区别(转载)
一.前言 在ES6(ES2015)出现之前,JavaScript中声明变量就只有通过 var 关键字,函数声明是通过 function 关键字,而在ES6之后,声明的方式有 var . let . c ...
- oracle 中 cursor 与refcursor及sys_refcursor的区别 (转载)
http://blog.csdn.net/gyflyx/article/details/6889028 引用一.显式cursor 显式是相对与隐式cursor而言的,就是有一个明确的声明的cursor ...
- php中global与$GLOBALS的用法及区别-转载
php中global 与 $GLOBALS[""] 差别 原本觉得global和$GLOBALS除了写法不一样觉得,其他都一样,可是在实际利用中发现2者的差别还是很大的! 先看下面 ...
- Java中List、Set和Map的区别--转载
List按对象进入的顺序保存对象,不做排序或编辑操作.Set对每个对象只接受一次,并使用自己内部的排序方法(通常,你只关心某个元素是否属于Set,而不关心它的顺序--否则应该使用List).Map同样 ...
随机推荐
- 【c++错误】类的语法错误 error c2533:constructors not allowed a return type(构造函数不允许返回一个类型)
今天编写类的程序的时候不小心把类后的分号忘写了,就出现上面的错误提示. 顺便复习下类的正确格式: class 类名 { public: //习惯上将公有类型放在前面,便于阅读 ……(外部接口) pro ...
- Math.random理解练习
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 【vue】vue的路由权限管理
前言: 最近闲来无事浏览各种博客,看到了一个关于路由权限的管理,觉得很有用,针对那个博客,准备自己写一个demo. 实现: 路由大致分为用户路由<特定用户才能浏览>和基本路由<所有用 ...
- Tab Key not working when using Xfce remote desktop
Xfce 远程桌面Tab键设置 Use CTRL-tab instead of tab The XFCE Terminal has kidnapped the tab key for a featu ...
- Difference between model.evaluate vs model.predict in Keras
The model.evaluate function predicts the output for the given input and then computes the metrics ...
- zTree实现权限列表简单实例
zTree的简单实例 zTree 是一个依靠jQuery 实现的多功能 "树插件".优异的性能.灵活的配置.多种功能的组合是 zTree 最大优点. zTree官网 zTreeAP ...
- Enum,Int,String的互相转换
Enum为枚举提供基类,其基础类型可以是除 Char 外的任何整型.如果没有显式声明基础类型,则使用Int32.编程语言通常提供语法来声明由一组已命名的常数和它们的值组成的枚举. 注意:枚举类型的基类 ...
- Mysql ibdata1简述
What is stored in ibdata1? 当启用innodb_file_per_table时,表存储在它们自己的表空间中,但共享表空间仍用于存储其他InnoDB的内部数据: 数据字典也就是 ...
- [OPENSSL下载][证书] OPENSSL将PFX证书转换为PEM格式
OpenSSL Convert PFX Convert PFX to PEM openssl pkcs12 -in certificate.pfx -out certificate.cer -node ...
- 【 PostgreSQL】后台周期执行函数实例(shell+crontab)
工作中常见函数后台周期执行的情况,Oracle有job实现,gp数据库可以通过shell+crontab实现.流程如下: gpadmin用户下创建函数sh脚本. 将sh挂在crontab任务上 ### ...