How To Store Data in Sql with UTC TimeZone when the system local in somwhere else

StackOverflow https://stackoverflow.com/questions/22744784

  •  24-06-2023
  •  | 
  •  

سؤال

i know there is not any way to change timezone of an application globaly, but is there any way to store datetimes in utc on sqlserver. right now any time i submit to server considerd as local. i can change that by ConvertToUTC() method in .net.

هل كانت مفيدة؟

المحلول 2

I Think easiest way is to use a function inside my application to fix time zone for me then send data to/From Server. I used 2 different functions :

 Friend tZone As TimeZoneInfo

 Public Function TimeZone_Get(ByVal time As DateTime, ByVal Tolocal As Boolean) As DateTime
    Dim t As DateTime = time

    If Tolocal = True Then
        t = TimeZoneInfo.ConvertTimeFromUtc(time, tZone)
    End If
    Return t
End Function

Public Function TimeZone_Set(ByVal time As DateTime, ByVal Islocal As Boolean) As DateTime
    Dim t As DateTime = time
    If Islocal = True Then
        t = TimeZoneInfo.ConvertTimeToUtc(time, tZone)
    End If
    Return t
End Function

نصائح أخرى

You could consider using the SQL function:

getUTCdate();

See here for more info: http://technet.microsoft.com/en-us/library/ms178635.aspx

Note the UTC datetime is derived from the Operating System of which the SQL Server is running.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top