Coldfusion 10 DateFormat Issue - coldfusion

I am using the DateFormat function to convert dates to this format: yyyy-mm-dd. This is the original format of the date: dd-mm-yyyy. Below is a snippet of the code:
<cfset newdate = #DateFormat(Trim(mydate), "yyyy-mm-dd")# />
The problem is that I get different results for different dates. For example:
If my original date is: 15-05-2013 (dd-mm-yyyy)
The result is: 2013-05-15 (yyyy-mm-dd)
However, if I change the input and:
The original date is: 01-05-2013 (dd-mm-yyyy)
The result is: 2013-01-05 (yyyy-dd-mm)
Any help or guidance as to what is wrong would be highly appreciated.

I disagree with the other answer. The real cause of the problem is that DateFormat is not designed to handle non-US date strings.
The standard CF date functions always use U.S. date parsing rules. That means when you pass in an ambiguous date string, like 01-05-2013, it is parsed according to U.S. English date conventions. In this case, month first ie "mm-dd-yyyy". So the result will always be January 5th, not May 1st.
In some cases you get lucky. With the string 15-05-2013, there is obviously no 15th month, so CF/java must swap the month and day automatically, rather than throwing an error. That is why it seems to handle some dd-mm-yyyy date strings correctly, but not others.
If you want to parse non-US date strings, you should use the LS (Locale Sensitive) date functions instead. However, according to the docs dashes ie "-" are not a standard date separator in most non-US locales: only Dutch and Portuguese (Standard). So you would either need to change the separator OR use one of those two locales when parsing the date:
lsDateFormat( myDate, "yyyy-mm-dd", "pt_PT")
Side note:
As an aside, DateFormat does expect a date object. However, like most functions in CF it is flexible enough to accept a date string as well. That allows you to use it as a lazy shortcut to convert from date string => date object => then back to (formatted) date string again. Using date objects is preferable (and you really should validate date strings as well) but that is another conversation altogether ...

The problem is that DateFormat expects a date object, and returns a string.
You're passing it a string, not a date. What you want to do is firstly turn your string (of 01-05-2013 etc) into a date object.
To do this I'd recommend using either ParseDateTime or LSParseDateTime, and/or LSDateFormat.
e.g.
<cfset originalDateString = "01-05-2013">
<!--- turn that into a Date --->
<cfset dateObject = ParseDateTime(originalDateString)>
<cfset newdateString = DateFormat(dateObject, "yyyy-mm-dd")>
Alternatively, if you know your string is always in a dd-mm-yyyy format, you could parse the string yourself, e.g. treat it as a list delimited by hyphens.
<cfset dd = listFirst(originalDateString, "-")>
<cfset mm = listGetAt(originalDateString, 2, "-")>
<cfset yy = listLast(originalDateString, "-")>

Related

I am having troubles converting string into date (error)

I am having troubles converting this fields into date.
Any thoughts on this?
The other dates are wrong, too:
11/7/2022 should be 2022-11-07, not 2022-07-11,
because
1/16/2023 must be a US date format MM/DD/YYYY
(there is no 16th month)
You can convert the string in Power BI "Using Locale ..." and then specifying English/US.

How to convert Particular timezone of date into GMT timezone using esql of MQ?

I have the date of particular timezone, and I want to convert it to the GMT timezone, and then it needs to be inserted into DB using esql of MQ. Please help to resolve this issue.
If you want to convert a date from a format to another, you can do the following :
DECLARE inDate DATE;
DECLARE outDate DATE;
DECLARE tempDate DATE;
DECLARE patternIN CHARACTER 'yyyy-MM-dd';
DECLARE patternOUT CHARACTER 'yyMMdd';
SET tempDate = CAST(inDate AS DATE FORMAT patternIN);
-- Convert input String as Date (should match patternIN)
SET outDate = CAST(tempDate AS CHARACTER FORMAT patternOUT)
-- Convert the date object to the desired date format
Of course you need to be able to define your date pattern. I know you might need to separate the DATE from the TIME, but the object are exactly the same. A quick example of a specific cast :
CAST(CURRENT_DATE AS CHARACTER FORMAT 'yyyy-MM-dd') || 'T' || CAST(CURRENT_TIME AS CHARACTER FORMAT 'HH:mm:SS')
This will generate a date in the XML format, e.g : 2019-08-28T16:46:32

ColdFusion CFQuery Looking for Month Only

ColdFusion 9 using Access Database stores the Dates such as 1/12/2015, 2/22/2015, 1/21/2015 etc...
I am looking to only get Month results such as January only, Feb only etc...
I can easily pull the year only If I need it using:
<cfquery name="f" datasource="ds">
select * from master
where eventdate like '%#yr#%'
</cfquery>
Where year is like 2015 - I can easily get year.
But when I switch to:
where eventdate like '%1/%'
I get a Syntax FROM error - I can't get only January. Likely simple solution - but I'm missing it.
Thx for any help.
Use real dates, in a sane format.
yyyy-mm-dd is pretty much the only sane date format, whereas mm/dd/yyyy is pretty much the most insane date representation out there. Don't use that representation for any purpose except end user output.
<cfset date = Now()> <!--- or some other actual date -->
<cfset dtfrom = DateFormat(date, "yyyy-mm-01")>
<cfset dtuntil = DateFormat(DateAdd("m", 1, dtfrom), "yyyy-mm-01")>
<cfquery name="f" datasource="ds">
select * from master
where eventdate >= '#dtfrom#' and eventdate < '#dtuntil#'
</cfquery>
The advantage of using this form >= ... and < ... is that if you have an index on eventdate, it can be used for that. Almost every other variant of expressing the same condition (for example Month(eventDate) = ...) can't use an index.
You escape special characters in access using brackets like so:
WHERE evendate LIKE '%1[/]%'
Also, if your field is an actual DATE (doesn't sound like it) you can tease it out with month() as in:
WHERE MONTH(eventDate) = 1
You might also be able to convert/cast your character field into a data using ASDATE like so:
WHERE MONTH(asDate(eventdate)) = 1
There's the limit of my ancient access experience. good luck. :)
You can use the Format method available in Access.
Try this: Format([eventdate],"yyyy/mm/dd") or if you just want the month and don't care about the year, your query could look like this:
select * from master
where Format([eventdate],"mm") = 1

Difference between dateformat() and createODBCDate() in ColdFusion

What is the difference between dateformat() and createODBCDate() in ColdFusion? Are these two functions the same or not? When do I need to use DateFormat() and when do I need to use createODBCDate()?
dateFormat() accepts a date and a format 'mask' and returns a string of the date, in the format passed.
For example, consider the following code:
mydate = dateFormat( now(), 'yyyy-mm-dd' );
Assuming the date is July 15, 2014 (which it was when I wrote this) the value of the variable named 'mydate' would be '2014-07-15' (without the quotes). So, you need to pass a date to the function.
createODBCDate() creates an actual date from the values passed - it does not format the date, it merely creates a date 'object'
dateFormat() is typically used to display a date in a user friendly manner. Try running this writeDump( now() ) to see what the default display looks like.
createODBCDate() is typically used when you need to pass a date to a SQL query. However, if you use cfqueryparam with a cf_sql_type that accepts a date, ColdFusion will handle converting the value (assuming it is a valid date) to a date that the database accepts and you do not need to use createODBCdate()
In 10+ years of doing ColdFusion, I have never used createODBCDate()

coldfusion - parsing a string date into datetime format

We receive an XML file with a date node as follows:
<createdDate>1/11/2008 7:04:28 a.m.</createdDate>
Dates are UK format dd/mm/yyy, so 1/11/2008 is 1st November 2008.
We run a coldfusion function to parse the xml and insert into the database. The relevant database field is of datetime datatype and needs to remain that way. How would I format this string representation of the date into a format the database will accept?
Not an ideal situation, but the format you are getting data especially dots in am/pm strings make it hard to read and on top of that it comes in UK Date format. This can help:
<cfset x="21/11/2008 7:04:28 p.m.">
<cfset x=Replace(x,".","","All")>
<cfset y=LSDateFormat(x,"mm/dd/yyyy","English (UK)")>
<cfoutput>
x====#x#
<br/>y===#y#
<cfset z=CreateDateTime(Year(y),month(y),day(y),hour(x),minute(x),second(x))>
z====#z#
<cfset someDatevare=LSParseDateTime(x,"English (UK)")>
</cfoutput>
EDIT As Leigh mentioned, removing periods or any other non-standard characters from the string and then LSParseDateTime will return a date time object.