[Gambas-user] Format function changes date value

T Lee Davidson t.lee.davidson at gmail.com
Sat May 19 20:43:35 CEST 2018


I don't get that 5 minute anomaly. With three blocks each defining zTestDate through Debug (Print), I get:
Main.Main.9: Test date: 5/4/2018 Formatted: 05/03/18 20:00 Thu
Main.Main.12: Test date: 5/4/2018 03:59:00 Formatted: 05/03/18 23:59 Thu
Main.Main.15: Test date: 5/4/2018 04:00:00 Formatted: 05/04/18 00:00 Fri


On 05/19/2018 01:48 PM, George wrote:
> (This was originally posted in gambas-user at lists.sourceforge.net <mailto:gambas-user at lists.sourceforge.net>
> I've since seen that list has been deprecated, so I'm reposting the pertinent issues here)
> 
> When using the Format function to format a date, the date gets offset by 1 day.  Here's an example:
> 
>   Dim zTestDate As String
>   Dim zTestResult As String
>   zTestDate = "5/4/2018"
>   zTestResult = Format(CDate(zTestDate), "mm/dd/yy ddd")
>   Debug "Test date: " & zTestDate & " Formatted: " & zTestResult
> 
> Result: Test date: 5/4/2018 Formatted: 05/03/18 Thu
> 
> This seems to have started since the last update was applied.
> 
>>T Lee Davidson <t.lee.davidson at gmail.com <mailto:t.lee.davidson at gmail.com>>
>>
>>CDate uses UTC and, without the time information in the string, it would store midnight. Format uses local time.
>>
>>So, anyone in a time zone behind UTC will get the previous day printed. Try this format string:
>>zTestResult = Format(CDate(zTestDate), "mm/dd/yy hh:nn ddd")
>>
>>Does the difference in hours match your timezone offset?
> 
> That is definitely part of the issue.  My local time is GMT-04, and here's what I get when specifying the time in my test:
> 
> Test date: 5/4/2018 03:59:00 Formatted: 05/03/18 Thu
> Test date: 5/4/2018 04:00:00 Formatted: 05/04/18 Fri
> 
> However, if I look at time in the results, the difference isn't exactly the offset:
> Test date: 5/4/2018 03:59:00 Formatted: 05/03/18 23:05:00  Difference: 4:54:00
> Test date: 5/4/2018 04:00:00 Formatted: 05/04/18 00:05:00  Difference: 3:55:00
> Test date: 5/4/2018 00:00:00 Formatted: 05/03/18 20:05:00  Difference: 3:55:00
> 
> The offset isn't an even number of hours. What's also odd is that this was never a problem before about a week ago.  This code
> is many years old, and the problem suddenly began occurring with compiled code.
> 
> 
> 
> 
> ----[ Gambas mailing-list is hosted by https://www.hostsharing.net ]----
> 

-- 
Lee


More information about the User mailing list