[Gambas-user] Format function changes date value
Gianluigi
bagonergi at gmail.com
Thu Jun 7 16:26:56 CEST 2018
2018-06-07 16:00 GMT+02:00 Benoît Minisini <g4mba5 at gmail.com>:
> Le 07/06/2018 à 15:52, George a écrit :
>
>> This is what broke my code:
>> Dim Dat1 As Date
>> Dat1 = "6/1/2018"
>>
>
> The problem is here. The automatic conversion is done by CStr(), i.e. it
> assumes that "6/1/2018" is UTC...
>
> Message("Dat1: " & Dat1 & " Formatted: " & Format(Dat1, "mm/dd/yyyy"))
>>
>
> ...but Format() prints the date in local timezone...
>
>
>> Result: Dat1: 06/01/2018 Formatted: 05/31/2018
>>
>
> ...And so you may have a difference.
>
> Each time you have a string converted to a date or the contrary, you must
> know which timezone Gambas assumes.
>
> To convert a string in local date, you have to use Val("6/1/2018"), or,
> faster, Date(2018,1,6).
>
>
>> I stand corrected on the DateDiff issue; my code was not using it because
>> it's counting days based on a schedule of working/non-working days. Since
>> I hadn't gotten past the 'Format' issue, I never dove into that part of the
>> code to see why it was breaking, other than seeing that day calculations,
>> which used to work, no longer did since the patch.
>>
>>
> For working / non-working days, I have written my own function (based on
> French days-off), but I didn't put it in Gambas yet.
>
> --
> Benoît Minisini
>
>
I believe that coercion should always be avoided.
You have passed a string to a date type:
Dim Dat1 As Date
Dat1 = "6/1/2018"
By writing Dat1 = CDate ("6/1/2018") the error would have been avoided.
Regards
Gianluigi
> ----[ Gambas mailing-list is hosted by https://www.hostsharing.net ]----
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.gambas-basic.org/pipermail/user/attachments/20180607/4e4a1aa3/attachment-0001.html>
More information about the User
mailing list