I am having a strange issue.
If Abs(DateDiff(CDate(IDDT), Now(), gb.Minute)) > 90 Then T = False
where IDDT = "07/18/2023 14:30:00"
Now() = "07/18/2023 14:31:12" ---- obviously this changes but that was current value
The result of the line is 482 which clearly is not minutes nor seconds. Is there a glitch or am I doing something wrong?
datediff not returning expected result
- BruceSteers
- Posts: 1598
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: datediff not returning expected result
i think Cdate uses UTC time so it will be different to your own timezones "Date" and "Now" resultssadams54 wrote: ↑Tuesday 18th July 2023 10:32pm I am having a strange issue.
If Abs(DateDiff(CDate(IDDT), Now(), gb.Minute)) > 90 Then T = False
where IDDT = "07/18/2023 14:30:00"
Now() = "07/18/2023 14:31:12" ---- obviously this changes but that was current value
The result of the line is 482 which clearly is not minutes nor seconds. Is there a glitch or am I doing something wrong?
If I use CDate here my results are -1 hour out.
We need to find the offset between our own timezones and UTC..
This fixed it for me...
'' Convert CDate() UTC value to local time
Public Sub UTC2Local(CDateResult As Date) As Date
' get the current time and make an American format string...
Dim d1 As Date = Now
Dim sUTC As String = Month(d1) & "/" & Day(d1) & "/" & Year(d1) & " " & Time(d1)
' get the offset of hours between the Date and CDate
Dim iOffset As Integer = DateDiff(CDate(sUTC), d1, gb.Hour)
' Now apply the offset to the given DateString and save it to the Date object
d1 = DateAdd(CDateResult, gb.Hour, iOffset)
Return d1 ' return the adjusted Date
End
then try this...
If Abs(DateDiff(UTC2Local(CDate(IDDT)), Now(), gb.Minute)) > 90 Then T = False
If at first you don't succeed , try doing something differently.
BruceS
BruceS
- BruceSteers
- Posts: 1598
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: datediff not returning expected result
That is the problem with using CDate and Date together.
Like i say there are workarounds if you have to use CDate with Date You may want to find another way though.
Like i say there are workarounds if you have to use CDate with Date You may want to find another way though.
If at first you don't succeed , try doing something differently.
BruceS
BruceS
Re: datediff not returning expected result
Maybe it can be changed like this:
Public Sub UTC2Local(CDateResult As Date) As Date
Dim d1 As Date = Now
d1 = DateAdd(CDateResult, gb.Hour, System.TimeZone \ 3600)
Return d1
End
Europaeus sum !
Amare memorentes atque deflentes ad mortem silenter labimur.
Amare memorentes atque deflentes ad mortem silenter labimur.
- BruceSteers
- Posts: 1598
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: datediff not returning expected result
ooh nice, that looks much better than my solution/workaround
I have never used so did not even know there was a System.Timezone property
so to make that much simpler/better version even shorter...
Public Sub UTC2Local(CDateResult As Date) As Date
Return DateAdd(CDateResult, gb.Hour, System.TimeZone \ 3600)
End
Or even better as System.Timezone is in seconds...
Public Sub UTC2Local(CDateResult As Date) As Date
Return DateAdd(CDateResult, gb.Second, System.TimeZone)
End
I just tested it and it works fine here
If at first you don't succeed , try doing something differently.
BruceS
BruceS
Re: datediff not returning expected result
thank you I tried the solution but it did not work.
Fear not I found a solution. It seems if I tried to send the results of cdate directly to the function I got something 8 hours in the future. If I assigned the cdate result to a date variable then send that to the function it works perfectly.
Thank you I would never have guessed somebody thought that time should be converted to UTC instead of local time like a normal person.
You guys are great.
Fear not I found a solution. It seems if I tried to send the results of cdate directly to the function I got something 8 hours in the future. If I assigned the cdate result to a date variable then send that to the function it works perfectly.
Thank you I would never have guessed somebody thought that time should be converted to UTC instead of local time like a normal person.
You guys are great.
- BruceSteers
- Posts: 1598
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: datediff not returning expected result
Yeah it does warn in the wiki for CDate command..
http://gambaswiki.org/wiki/lang/cdate
And ONLY if the argument for CDate is a string. if you send a Date object it does not convert
Thanks to Vuott we have a nice simple way to convert using System.Timezone
(you'll just have to figure out the way to make either local to UTC and also UTC to local)
If your country's localization uses the same date format as american (m/d/y) then you should not need CDate and can just use normal localized date functions.
http://gambaswiki.org/wiki/lang/cdate
So it is not a conversion to UTC but more of an "expectation" that the given string is UTC.gambas wiki wrote: Be careful! The current localization is not used by this function.
In other words, if the expression is a string, it is assumed to be a date written in american format at UTC time.
And ONLY if the argument for CDate is a string. if you send a Date object it does not convert
Thanks to Vuott we have a nice simple way to convert using System.Timezone
(you'll just have to figure out the way to make either local to UTC and also UTC to local)
If your country's localization uses the same date format as american (m/d/y) then you should not need CDate and can just use normal localized date functions.
If at first you don't succeed , try doing something differently.
BruceS
BruceS