Here is some visual basic code for you!
You receive a ZULU time and want to store it in your local time?
Dim MyZulu As New Date(year,month,day,hr,min,sec,DateTimeKind.Utc) Dim MyLocal As DateTime = MyZulu.ToLocalTime()
Or is someone or some API asking you for the next format in return, and all you have is a DateTime with your local time in it?
*UTC + offset as (YYYY-MM-DDThh:mm:ss+hh:mm)
Well, you could convert it, like so:
Dim TZ As TimeZoneInfo = TimeZoneInfo.Local Dim MyZulu As DateTime = TimeZoneInfo.ConvertTimeToUtc(MyLocal, TZ) Dim strTime As String = MyZulu.ToString("yyyy-MM-ddTHH:mm:ss").ToString() & MyLocal.ToString("zzz").ToString()
But what they actually expect is a timestamp, in your local time, plus the offset to UTC. So simply use:
Dim strTime As String = MyLocal.ToString("yyyy-MM-ddTHH:mm:ss").ToString() & MyLocal.ToString("zzz").ToString()
Consider this about timestamps:
TIMESTAMP WITH TIME ZONE values are considered identical if they represent the same instant in UTC, regardless of the
TIME ZONE offsets stored in the data. For example, the following expressions have the same value:
TIMESTAMP '1999-01-15 8:00:00 -8:00' TIMESTAMP '1999-01-15 11:00:00 -5:00'
And if you get an error:
Managed Debugging Assistant ‘DateTimeInvalidLocalFormat’ : ‘A UTC DateTime is being converted to text in a format that is only correct for local times. This can happen when calling DateTime.ToString using the ‘z’ format specifier, which will include a local time zone offset in the output. In that case, either use the ‘Z’ format specifier, which designates a UTC time, or use the ‘o’ format string, which is the recommended way to persist a DateTime in text. … ‘
…then you most likely mixed up MyLocal and MyZulu.
You can not get the offset if the DateTime contains a UTC value.
Have a nice day!