Can somebody please explain this behavior : ``` {"...
# cfml-general
s
Can somebody please explain this behavior :
Copy code
{
  "VALUE": "2024-04-04T14:10:39+00:00",
  "d2": "2024-04-04 16:10:39"
}
d2 =
dateTimeFormat(value, "yyyy-mm-dd HH:nn:ss")
j
Good morning! Two things for you: 1. You're trying to output time information also, so you will want to use DateTimeFormat(). 2. DateTimeFormat defaults* to using the system's timezone as the basis for the calculation. See the timezone argument here: https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-c-d/DateTimeFormat.html
s
I meant DateTimeFormat 🙂 typo.
j
I figured, but better to be thorough. 🙂
s
I figured it was related to my system's timezone, but I could not wrap my head around it. Thank you for the feedback.
To retain a date-object, I suppose i could also use the
dateConvert("local2utc", date)
fn?
j
As in before passing it into DateTimeFormat()?
s
yes
j
That does appear to handle your situation. It's interesting to note that it appears that CF handles it by calculating the timezone offset, adjusting the date, and then dropping the timezone information from the timestamp.
{ts '2024-04-04 14:10:39'}
s
Both interesting and annoying 😄.
q
@James Harris -- it didn't show the timezone because he didn't have it in his DateTimeFormat string.
j
In his original example, value has it with the +00:00. Dropping that from the string prior to the DateTimeFormat() also works as desired.
q
if he would have used the "full" or "iso" formats, it would have shown the timezone in his output
The incoming string had a timezone set (UTC), and CF correctly converted it to the timezone of the server. Then when he output the date/time, it used the local timezone
the output didn't SHOW the timezone because he didn't specify it. He also didn't tell it what timezone to adjust the time to -- he could have set the timezone parameter of the dateTimeFormat to "ETC/UTC" to keep the +0:00
j
Good additional information. Thank you, quetwo.
s
Thank you both for the feedback / input!
q
Pretty much, if you explicitly set the timezone /anywhere/ in the lifecycle of the variable, you then need to explicitly set it /everywhere/. If you are always timezone agnostic, then it will always reference your local timezone. The only pitfall with that is if your database sets it explicitly somewhere (like the MySQL driver likes to do), then you might still need to deal with it.