date format - Coldfusion 10 DateFormat Issue -
i using dateformat function convert dates format: yyyy-mm-dd. original format of date: dd-mm-yyyy. below snippet of code:
<cfset newdate = #dateformat(trim(mydate), "yyyy-mm-dd")# /> the problem different results different dates. example:
- if original date is:
15-05-2013(dd-mm-yyyy) - the result is:
2013-05-15(yyyy-mm-dd)
however, if change input and:
- the original date is:
01-05-2013(dd-mm-yyyy) - the result is:
2013-01-05(yyyy-dd-mm)
any or guidance wrong highly appreciated.
i disagree other answer. real cause of problem dateformat not designed handle non-us date strings.
the standard cf date functions always use u.s. date parsing rules. means when pass in ambiguous date string, 01-05-2013, parsed according u.s. english date conventions. in case, month first ie "mm-dd-yyyy". result january 5th, not may 1st.
in cases lucky. string 15-05-2013, there no 15th month, cf/java must swap month , day automatically, rather throwing error. why seems handle dd-mm-yyyy date strings correctly, not others.
if want parse non-us date strings, should use ls (locale sensitive) date functions instead. however, according docs dashes ie "-" not standard date separator in non-us locales: dutch , portuguese (standard). either need change separator or use 1 of 2 locales when parsing date:
lsdateformat( mydate, "yyyy-mm-dd", "pt_pt") side note:
as aside, dateformat expect date object. however, functions in cf flexible enough accept date string well. allows use lazy shortcut convert date string => date object => (formatted) date string again. using date objects preferable (and should validate date strings well) conversation altogether ...
Comments
Post a Comment