Search code examples
sqltimezonelocalutc

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


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.


Solution

  • 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