根据sqlServer中的客户端时区存储当前日期时间 [英] Store Current Datetime according to client side timezone in sqlServer

查看:44
本文介绍了根据sqlServer中的客户端时区存储当前日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据类型为 datetime 的 sql 表中有 dateTimeAuth 列,它的默认值是 (getdate()),如果我在离线模式下运行我的项目,它会返回当前时间.当我将它上传到实时服务器时,它会存储默认时区 GMT 的当前时间.我希望根据 IST 存储值.怎么办?

I have dateTimeAuth column in sql table of datatype datetime and its default value is (getdate()) which returns me current time if i run my project at offline mode. When i uploaded it to live server it stores current time of default timezone GMT. I want value to be stored according to IST. How can it be done ?

根据 GMT 存储的值:8/26/2010 1:02:28 PM根据 IST 的实际时间 :8/26/2010 7:02:28 PM

Value stored accoding to GMT : 8/26/2010 1:02:28 PM Actual time accodring to IST :8/26/2010 7:02:28 PM

推荐答案

getdate() 将始终返回服务器所在的日期和时间.如果您想要来自用户时区的日期和时间,则需要从应用程序发送日期并将其作为常规 dateTime 变量插入

getdate() will always return the date and time where the server is located. If you want the date and time from your user's timezone, you need to send the date from the application and insert it as a regular dateTime variable

但是如果您使用的是 SQL 2008,则可以使用

but if you are on SQL 2008, you can use

SELECT SYSDATETIMEOFFSET()

获取您所在时区的日期.您还可以向其中添加日期(UTC + 5:30 = IST)

to get the date on your timezone. You can also add dates to it (+5:30 from UTC = IST)

SELECT SWITCHOFFSET(SYSDATETIMEOFFSET(), '+05:30')

这篇关于根据sqlServer中的客户端时区存储当前日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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