更新记录时的日期时间问题 [英] Datetime Issue while updating record

查看:97
本文介绍了更新记录时的日期时间问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我试图保存来自移动应用程序的记录,并且从设备发送数据的格式为

Hello All,
I trying to save record from mobile application and the format in which data is send from device is

2012-10-09T13:04:54+05:30

,并且在将记录保存到数据库的电话服务中,我正在c#中使用DateTime.Parse

, and in phone service that saves the record in database i am parsing the data using DateTime.Parse

(date1)

解析数据.但是,当数据保存在sql server中时,它可以节省1天的时间(例如:如果发送10/9/2012,则可以保存10/8/2012).

我尝试实现(因为我的数据库服务器在美国)

in c#. but when data is saved in sql server it is saving 1 day back (like : if a send 10/9/2012 it saves 10/8/2012).

i tried to implement ( as my database server is in US)

TimeZoneInfo.ConvertTime(date1, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"));



但这对我没有用.谁能建议我出什么问题了.

谢谢



But it did''t work for me. Can anybody suggest me what is going wrong.

Thanks

推荐答案

您应该使用UTC(世界标准时间)
更新时间. 这是标准的国际时间,其中所有时区都表示为偏移时间.
要从UTC计算本地时间,请添加时区偏移量,然后在夏令时生效的情况下再添加一个小时.

获取世界标准时间的时间

You should be updating the time using UTC (Coordinated Universal Time)
It is the standard international time in which all timezones are expressed as offset of.
To compute local time from the UTC, add the timezone offset and then add an additional hour if daylight savings time is in effect.

To Get the Current time in UTC

C#
DateTime currentTime = DateTime.UtcNow; 

SQL 
DECLARE @currentTime datetime
SET @currentTime = GETUTCDATE() 



要将UTC转换为当前时间



To convert UTC to current time

C#

DateTime currentServerTime = DateTime.UtcNow.ToLocalTime();
currentServerTime = ((DateTime) Eval("YourDate")).ToLocalTime();


这篇关于更新记录时的日期时间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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