I am converting UTC date into Oracle-friendly date...
# cfml-beginners
e
I am converting UTC date into Oracle-friendly date as this:
<cfset x = #dateTimeFormat(lsParseDateTime(DateConvert("UTC2Local", "2021-12-09T15:11:32.273Z")), 'MM/DD/YYYY hh:nn:ss')#>
<cfoutput>#x#</cfoutput>
and getting this: 12/08/2021 111132 How do I convert back to "*2021-12-09T151132.273Z*" format?
a
First things first, separate the concepts of "a string that looks like a date" and "a date" in your mind. • this returns a date:
lsParseDateTime(DateConvert("UTC2Local", "2021-12-09T15:11:32.273Z"))
. This is what you generally want to have in your code. •
dateTimeFormat
takes a date and converts it into a string. You only ever really want to do that at the very last moment when the date value exits your app (html, json, some DBs, etc)
So you have date:
myDate = lsParseDateTime(DateConvert("UTC2Local", "2021-12-09T15:11:32.273Z"))
And you want to format it like:
"2021-12-09T15:11:32.273Z"
(which is a string)
To format a date, one uses
dateTimeFormat
, as you already seem to know. So... possibly refine your question. Which part of formatting a date into that format are you having trouble with?
b
First, "DD" is the day of year, not the day of month. I'm sure you wanted "dd" instead. Next, the original milliseconds (.273) are gone and you can't get those back from the formatted value because it doesn't contain them. So, the best you can do is format what you DO have into what you want it to look like so something like... dateTimeFormat(x, "yyyy-mm-dd HHnnss.LLL").replace(" ", "T") & "Z"
👍 1
m
just a note, in general, be careful with DD vs dd, lucee and acf 2021 treat DD as day in year. I suspect the challenge is around text mixed in the formatting. dateTimeFormat(x, "yyyy-mm-dd'T'HHnnss.lll'Z'"), especially when it conflicts with formats
❤️ 1
b
facepalm... TIL that wrapping T or Z in single quotes is how you escape them in a date/time mask. Thanks for that.
✅ 3
a
I only just found that out too (not via here... googled some blog article)
Also @epipko are you sure you want to be using
DateConvert
in there? You said yer starting with a UTC date anyhow?
e
Thanks for replies. I am trying to generate API call. The example they have is using CURL:
curl
-H "X-Api-Token: ABC123"
-H "X-Api-Version: 2020-08"
"<https://api.returnly.com/returns.json?include=return_line_items,shipping_labels>
&status=refunded
&min_updated_at=2017-02-22T20:28:13.000Z
&max_updated_at=2017-02-22T20:28:13.000Z
&min_refunded_at=2017-02-22T20:28:13.000Z
&max_refunded_at=2017-02-22T20:28:13.000Z
&page=1
&per_page=1"
I am looking for a way to convert last downloaded return date (which I know) and pass it into the query param
a
Although the escaping is clearly documented here: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html @Mark Takata (Adobe) / @saghosh should really have that in the docs here though: https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-c-d/DateTimeFormat.html I have submitted an addition for the equiv on cfdocs
e
#dateTimeFormat(DateConvert("UTC2Local", return_date),'MM/DD/YYYY hh:nn:ss')#
and
#dateTimeFormat(lsParseDateTime(DateConvert("UTC2Local", "2021-12-09T15:11:32.273Z")), 'MM/DD/YYYY hh:nn:ss')#
returning the same result
a
Why don't you just pass the date? The DB driver already knows how to handle date objects. You don't need to convert them to strings.
e
I have no issues inserting dates into database. I am asking about generating dates "from database" in UTC format so I can pass them into API call (see above)
a
Yeah sorry, your wording was vague with the "and pass it into the query param". But yeah I see your HTTP call now, got it
e
when I try to reverse it as
<cfset utcNow = #dateConvert("Local2UTC", now())#>
I am getting {ts '2022-04-19 183148'}, there is no time zone
Thanks Matt, your solution works