-
October 20th, 2005, 10:11 AM
#1
Date Conversion
Hello,
I'm really stuck on this one. I need to be able to convert a UTC date into the local date and time. The value that I read is the number of seconds since midnight on january 1, 1970. I need to convert that number into the local date and time. For example, I'm given the number 1114706663. Does anyone know how I can convert this to the local date and time?
Thank you so much!
Steph
-
October 20th, 2005, 10:45 AM
#2
Re: Date Conversion
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
October 20th, 2005, 10:54 AM
#3
Re: Date Conversion
Thanks for the links. I actually was looking at these earlier. The problem I am having with these functions is that they take a date value as a parameter. The value I have to work with is an integer value ie:1114706663, that represents the number of seconds since midnight jan 1, 1970.
Here is the one function...
Code:
' Convert UTC time to local time for current time zone
Public Function UTCtoLocal(ByVal tDate As Date) As Date
Dim tzi As TIME_ZONE_INFORMATION
Dim stUTC As SYSTEMTIME
Dim stLocal As SYSTEMTIME
Dim lRes As Long
lRes = GetTimeZoneInformation(tzi)
stUTC.wYear = Year(tDate)
stUTC.wMonth = Month(tDate)
stUTC.wDay = Day(tDate)
stUTC.wHour = Hour(tDate)
stUTC.wMinute = Minute(tDate)
stUTC.wSecond = Second(tDate)
stUTC.wMilliseconds = 0
lRes = SystemTimeToTzSpecificLocalTime(tzi, stUTC, stLocal)
UTCtoLocal = DateSerial(stLocal.wYear, stLocal.wMonth, stLocal.wDay) + TimeSerial(stLocal.wHour, stLocal.wMinute, stLocal.wSecond)
End Function
Am I overlooking something?
Thanks,
Steph
-
October 20th, 2005, 11:05 AM
#4
Re: Date Conversion
Code:
Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Private Declare Function SystemTimeToTzSpecificLocalTime Lib "kernel32" ( _
lpTimeZoneInformation As Any, _
ByRef lpUniversalTime As SYSTEMTIME, _
ByRef lpLocalTime As SYSTEMTIME) As Long
Private Declare Function VariantTimeToSystemTime Lib "oleaut32.dll" ( _
ByVal vTime As Double, _
ByRef lpSystemTime As SYSTEMTIME) As Long
Function MakeLocalDateTime(ByVal nSecondsSince1970 As Double) As Date
Dim dateUTC As Date, stUTC As SYSTEMTIME, stLTC As SYSTEMTIME
dateUTC = DateAdd("s", nSecondsSince1970, #1/1/1970#)
Call VariantTimeToSystemTime(dateUTC, stUTC)
Call SystemTimeToTzSpecificLocalTime(ByVal 0&, stUTC, stLTC)
MakeLocalDateTime = DateSerial(stLTC.wYear, stLTC.wMonth, stLTC.wDay) _
+ TimeSerial(stLTC.wHour, stLTC.wMinute, stLTC.wSecond)
End Function
Private Sub Form_Load()
MsgBox MakeLocalDateTime(1114706663)
End Sub
Hope it will help
-
October 20th, 2005, 11:38 AM
#5
Re: Date Conversion
Originally Posted by StephanieHarris
Am I overlooking something?
You definetly are overlooking the very basic functions present in VB.
Why make things so complex when you could do it in a single line.
Code:
Debug.Print DateAdd("s",1114706663,"01-Jan-1970")
This will print 4/28/2005 4:44:23 PM and code posted by rxbagain also returns the same value. Why use so many API's and so many lines of code.
I would have rather taken a look at the date functions that are available in VB instead of taking the API route. I only use APIs when the kind of functionality that I want is not present in VB.
Ps: No offenses.
-
October 20th, 2005, 02:30 PM
#6
Re: Date Conversion
I'm not that advanced, so I could be wrong but it looks like the code posted by rxbagain will also take into account what time zone you are in.
Is this correct?
Thanks,
Steph
-
October 20th, 2005, 02:34 PM
#7
Re: Date Conversion
Looks that way. I wonder though, why would the zone matter?
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
October 21st, 2005, 02:57 AM
#8
Re: Date Conversion
Originally Posted by StephanieHarris
I'm not that advanced, so I could be wrong but it looks like the code posted by rxbagain will also take into account what time zone you are in.
Well I would still say No. The case of time zones doesn't arise here. You actually need a date that is 1114706663 seconds since 01-Jan-1970. So whether you are in US or Japan won't matter here. It will always give you the same result.
-
October 21st, 2005, 07:44 AM
#9
Re: Date Conversion
I answered the question with the assumption that the time is translated to UTC before he arrived at the number (number of seconds) and what he wants is to convert it in the local time value. And so what I did. I assumed that the date value came from other source (other pc or even other country), translated to UTC and was sent to the program for processing.
StephanieHarris, how/where did you get the number? Is it computed base on UTC?
Last edited by rxbagain; October 21st, 2005 at 08:42 AM.
-
October 21st, 2005, 08:28 AM
#10
Re: Date Conversion
It is based on UTC and in fact, it is coming from a different pc. I'm reading this value from a sql server database that could be located anywhere (not on the same machine as my program for sure)
Thanks,
Steph
-
October 21st, 2005, 08:41 AM
#11
Re: Date Conversion
Originally posted by StephanieHarris
It is based on UTC and in fact, it is coming from a different pc. I'm reading this value from a sql server database that could be located anywhere (not on the same machine as my program for sure)
Then the timezone conversion is really needed in your case. If you need to display it as UTC date, you would just use DateAdd, but in your case you really need to do that.
-
October 21st, 2005, 09:07 AM
#12
Re: Date Conversion
Originally Posted by rxbagain
Then the timezone conversion is really needed in your case. If you need to display it as UTC date, you would just use DateAdd, but in your case you really need to do that.
Well in this case you will have to convert it to the Locat Date/Time.
How could I miss UTC in the First post itself? Confusion confuses
-
October 21st, 2005, 09:20 AM
#13
Re: Date Conversion
Thanks for all of the help. I'm on my way now!!!
Steph
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|