I have a strange issue with dateFormat, on trycf t...
# cfml-general
h
I have a strange issue with dateFormat, on trycf this code
Copy code
<cfoutput>#DateTimeFormat("31.07.2024", "yyyy-MM-dd'T'HH:nn:ss'Z'", "GMT")#</cfoutput>
returns: 2024-07-31T000000Z (in Lucee and ACF) But on my machine it returns 2024-07-30T220000Z Does somebody know the reason? Is there another method (maybe using Java) to format Dates like 31.07.2024 (german), 07/31/2024 (english), 31/07/2024 (french) in the format "yyyy-MM-dd'T'HHnnss'Z'"? Using
Copy code
<cfoutput>#LsDateTimeFormat("31.07.2024", "yyyy-MM-dd'T'HH:nn:ss'Z'", "German (Standard)", "GMT")#</cfoutput>
doesn't work too an my machine, but works on trycf
a
doesn't work too an my machine, but works on trycf
Does it error?
h
No, on my machine the result is wrong - 2024-07-30T220000Z instead of 2024-07-31T000000Z
In ACF the Java Default Locale is "de_DE"
a
OK, so LsDateTimeFormat takes an datetime object. You are passing it a string so it's having to attempt parse it for you - that could be causing issues. Does this work?
Copy code
<cfset mydate = createDateTime( 2024, 07, 31, 12, 11, 10, 09 )>
<cfoutput>
#LsDateTimeFormat(mydate, "yyyy-MM-dd'T'HH:nn:ss'Z'", "German (Standard)", "GMT")#
</cfoutput>
h
Cool. using createDateTime works! Thanks
👍 1
😀 1
So I need to manipulate my dates like 31.07.2024 first into createDateTime parts
a
Dates as strings are just a huge pain and I really wish CFML didn't try and 'guess' what you wanted. You should always have to tell it what the mask is to be sure it doesn't trip you up. So you can either use createDateTime or lsparsedatetime with an explicit mask https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-l/lsparsedatetime.html
👍 2
d
Is there not an
Instant
yet?
a
nope - need to go to java land for that
I mean - humans struggle to read dates. For example
1/2/2024
is that 1st of Feb or 2nd of Jan? I mean obviously any reasonable person would see that as 1st of Feb in the year 2024 but hey ho 😁
😆 2
🤣 1
d
yyyy-mm-dd
ftw!
🎯 2
This little chunk from the excellent SO entry about instants sums it up nice:
Nearly all of your backend, database, business logic, data persistence, data exchange should all be in UTC. But for presentation to users you need to adjust into a time zone expected by the user. This is the purpose of the
ZonedDateTime
class and the formatter classes used to generate String representations of those date-time values.
UTC ftw for storage at least
👍 2
2
👍🏻 1
a
agreed