-- my current uk time is 2014-01-09 10:43:00 +0
DECLARE @userLocalTimeChina datetimeoffset(4) = '2014-01-09 18:43:00 +08:00';
DECLARE @userLocalTimeLasVagas datetimeoffset(4) = '2014-01-09 02:43:00 -08:00';
DECLARE @userUTCTime datetimeoffset(4) = '2014-01-09 10:43:00 +00:00';
Declare @systemDateTimeOffsetWhereServerInUK datetimeoffset(4) = SYSDATETIMEOFFSET();
Declare @systemDateTimeWhereServerInUK datetime = GETDATE();
print @systemDateTimeOffsetWhereServerInUK
print @systemDateTimeWhereServerInUK
-- for demo, it gives this
SET @systemDateTimeOffsetWhereServerInUK = '2014-01-09 10:43:00 +00:00';
SET @systemDateTimeWhereServerInUK = '2014-01-09 10:43:00'
Declare @systemDateTimeOffsetWhereServerInNY datetimeoffset(4) = '2014-01-09 05:43:00 -05:00';
Declare @systemDateTimeWhereServerInNY datetime = '2014-01-09 05:43:00';
-- ALL THESE TIMES should be the same.
--Using HOURS here to show the difference, but if we usually use DAYS, this simply means people can 'see' things or 'where clauses' go wrong at certain hours of the day
print 'Compare user LOCAL time to server offset and not offset date time, in uk then new york'
print DATEDIFF(HH, @userLocalTimeChina, @systemDateTimeOffsetWhereServerInUK); -- Correct
print DATEDIFF(HH, @userLocalTimeChina, @systemDateTimeWhereServerInUK); -- The result is correct, but only because server is in UK and no daylight savings time, so will be INCORRECT and CORRECT depending on time of year!!!
print DATEDIFF(HH, @userLocalTimeChina, @systemDateTimeOffsetWhereServerInNY); -- CORRECT
print DATEDIFF(HH, @userLocalTimeChina, @systemDateTimeWhereServerInNY); -- INCORRECT RESULT
print 'Compare user UTC time to server offset and not offset date time, in uk then new york'
-- compare Users UTC time offset with system offset produces CORRECT result most of the time, in the uk
print DATEDIFF(HH, @userUTCTime, @systemDateTimeOffsetWhereServerInUK); -- CORRECT
print DATEDIFF(HH, @userUTCTime, @systemDateTimeWhereServerInUK); -- The result is correct, but only because server is in UK and no daylight savings time, so will be INCORRECT and CORRECT depending on time of year!!!
print DATEDIFF(HH, @userUTCTime, @systemDateTimeOffsetWhereServerInNY); -- CORRECT
print DATEDIFF(HH, @userUTCTime, @systemDateTimeWhereServerInNY); -- INCORRECT RESULT
--CONCLUSION: ALWAYS COMPARE OFFSET TO OFFSET. OR UTCDATETIME TO UTCDATETIME. otherwise it's totaly dependant on the deploy location time zone if the solution will work.

Never use GetDate() when comparing date timesoffsets, use SYSDATETIMEOFFSET()的更多相关文章

  1. 【Swift】日期比较函数 记录下 Comparing date in Swift

    Add this code to your project and comparing dates is easier than ever 扩展NSDATE //swift 3.0.2 extensi ...

  2. JavaScript Date对象

    本篇主要介绍 Date 日期和时间对象的操作. 目录 1. 介绍:阐述 Date 对象. 2. 构造函数:介绍 Date 对象的构造函数new Date()几种方式. 3. 实例方法:介绍 Date ...

  3. Java 时间类-Calendar、Date、LocalDate/LocalTime

    1.Date 类 java.util.Date是一个"万能接口",它包含日期.时间,还有毫秒数,如果你只想用java.util.Date存储日期,或者只存储时间,那么,只有你知道哪 ...

  4. 时间格式转换—将后台返回的/Date(1448954018000)/格式转换为正常的时间格式

    用JS实现方法: function ChangeDateFormat(cellval) { )); < ? ) : date.getMonth() + ; ? " + date.get ...

  5. Javascript格式化json返回的时间(/Date(1482747413000)/)

    //时间处理,类似/Date(1482747413000)/的形式,得到xxx年xx月xx日 11:11:11 function ChangeDateFormat(jsondate) { jsonda ...

  6. #9.6课堂JS总结#变量作用域 date()对象 math()对象

    一.变量的作用域 1.JavaScript的作用域链 首先看下下面这段代码: <script type="text/javascript"> var rain = 1; ...

  7. JS date常用代码积累

    Date.prototype.Format = function(fmt) { var o = { "M+" : this.getMonth()+1, //月份 "d+& ...

  8. JSON.stringify转换Date不正确的解決方法

    JSON.stringify转换Date不正确的原因:国际时区(UTC)和中国时区(GMT)的原因,东八区+8等于国际时区. 解决方法,重新Es5的Date.prototype.toJSON方法,代码 ...

  9. js中转换Date日期格式

    在javascript中直接输出Date得到的结果是这样的: function date(){ var date = new Date(); alert(date); } 结果是:Mon Jun 15 ...

随机推荐

  1. 一、VSTO概述

    一.什么是VSTO? VSTO = Visual Studo Tools for Office,是.net平台下的Office开发技术.相对于传统的VBA(Visual Basic Applicati ...

  2. 窗口 namedWindow(), destroyWindow(), destroyAllWindows()[OpenCV 笔记6]

    void namedWindow(const string& winname, int flags=WINDOW_AUTOSIZE); 创建一个窗口.imshow直接指定窗口名,可以省去此函数 ...

  3. (转)Libevent(5)— 连接监听器

    转自:http://name5566.com/4220.html 参考文献列表:http://www.wangafu.net/~nickm/libevent-book/ 此文编写的时候,使用到的 Li ...

  4. yum 安装 依赖报错

    今天使用yum安装的时候 报错: Error: Multilib version problems found. This often means that the root cause 应该是yum ...

  5. Chrome浏览器报错:Origin null is not allowed by Access-Control-Allow-Origin.

    问题:Chrome浏览器报错:Origin null is not allowed by Access-Control-Allow-Origin. 原因:.js文件中使用load()方法,而Chrom ...

  6. jquery 实现复选框单选

    $(function() { $(':checkbox[name=复选框name值]').each(function() {  $(this).click(function() {   if ($(t ...

  7. 曾经的10道JAVA面试题

    1.HashMap和Hashtable的区别. 都属于Map接口的类,实现了将惟一键映射到特定的值上.HashMap 类没有分类或者排序.它允许一个null 键和多个null 值.Hashtable ...

  8. css3动画使用技巧之—JQ配合css3实现轮播之animation-delay应用

    <!DOCTYPE html> <html> <head> <title>css3动画使用技巧之—JQ配合css3实现轮播之animation-dela ...

  9. xml转array

    1.字串 $xml = simplexml_load_string($data);$array = json_decode(json_encode($xml),TRUE); 2.文件$xml = si ...

  10. 快速设置IP的脚本

    @echo off cls ::set NAME="本地连接" set NAME="无线网络连接" set IP=192.168.1.55 set MASK=2 ...