Dateformat weirdness

86 views
Skip to first unread message

Gavin Baumanis

unread,
May 7, 2015, 5:09:20 AM5/7/15
to lu...@googlegroups.com
Hi Everyone,
I am having a WTF moment...

I have a form where the date is entered in AUSTRALIAN format DD/MM/YYYY

On the form it is submitted to I have the following code;
form: #form.startdate#<br />
US: #dateformat(form.startdate, "MM-DD-YYYY")#<br />
AU: #dateformat(form.startdate, "DD-MM-YYYY")#<br />
ISO: #dateformat(form.startdate, "YYYY-MM-DD")#<br />

here is the output.

form: 01/07/2015
US: 01-07-2015
AU: 07-01-2015
ISO: 2015-01-07

The Windows server locale is set to Australia.
The Lucee region is set to australia - and shows the date and time in Australian format correctly.

Can anyone explain what I am doing wrong?
Is there a bug?

Thanks!



Andrew Dixon

unread,
May 7, 2015, 6:02:47 AM5/7/15
to lu...@googlegroups.com
Try LSDateFormat instead. I believe only the "LS" function use the locale.

Kind regards,

Andrew

--
You received this message because you are subscribed to the Google Groups "Lucee" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lucee+un...@googlegroups.com.
To post to this group, send email to lu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lucee/470881f7-3f82-438a-a895-bff2c50005e9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Julian Halliwell

unread,
May 7, 2015, 6:23:35 AM5/7/15
to lu...@googlegroups.com
Or parse the form value to an Aussie date before passing it to the
standard formatting functions:

form.startdate = LsParseDateTime( form.startdate );

Nando Breiter

unread,
May 7, 2015, 7:09:17 AM5/7/15
to lu...@googlegroups.com
What may be happening is that by default, dateformat() interprets a date that could be MM/DD or DD/MM in the American way, MM/DD. What I don't know is if Lucee will correctly interpret the date if you use LSDateFormat.

I've run into this on ACF, and use the date widget from jQuery to work around it, basically to ensure a date is always submitted in the unambiguous YYYY-MM-DD format, and displayed to the user consistently and correctly in the DD-MM-YYYY format. I'd do the same on Lucee, even if I found a correct pathway through this morass, simply to ensure that dates are always represented in a consistent way, especially for functions like DateAdd(), for example.





Aria Media Sagl
Via Rompada 40
6987 Caslano
Switzerland

+41 (0)91 600 9601
+41 (0)76 303 4477 cell
skype: ariamedia

--
You received this message because you are subscribed to the Google Groups "Lucee" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lucee+un...@googlegroups.com.
To post to this group, send email to lu...@googlegroups.com.

Andrew Dixon

unread,
May 7, 2015, 8:35:29 AM5/7/15
to lu...@googlegroups.com
Here is a trycf.com gist to show that using LSDateFormat works:


Kind regards,

Andrew

Jean Moniatte

unread,
May 7, 2015, 9:01:31 AM5/7/15
to lu...@googlegroups.com
I would use the lsIsDate and lsParseDateTime functions, they are made just for this (just make sure to set the correct locale) :


    form.startDate = '01/07/2015';
    
    setLocale('english (us)');
    echo('<pre>#getLocale()#</pre>');
    if (lsIsDate(form.startDate))
    {
        echo('<pre>#lsParseDateTime(form.startDate)#</pre>');  // {ts '2015-01-07 00:00:00'}
    }
    else
    {
        echo('<pre>Invalid input</pre>');
    }
    
    setLocale('english (australian)');
    echo('<pre>#getLocale()#</pre>');
    if (lsIsDate(form.startDate))
    {
        echo('<pre>#lsParseDateTime(form.startDate)#</pre>');  // {ts '2015-07-01 00:00:00'}
    }
    else
    {
        echo('<pre>Invalid input</pre>');
    }



Once you have extracted a datetime object, use lsDateFormat in your views.

Thanks,
Jean

Gavin Baumanis

unread,
May 7, 2015, 7:01:32 PM5/7/15
to lu...@googlegroups.com
OK lsDateFormat works.

Thanks.

"I:" think it is a bug that dateformat does not honour the system locale properly...
But I will leave that for other peopke to argue about.

The code I am using has not changed from an earlier version of ACF where it was working successfully.

So at best there is an inconsistency - at worst there is a bug.

Thanks.
Gavin.

Michael Offner

unread,
May 8, 2015, 2:18:56 AM5/8/15
to lucee
DateFormat ignore your locale settings completely, this function only using the US date locale, let me explain what is exactly going on in your examples.

dateformat("01/07/2015", "MM-DD-YYYY")
the date string with the first argument is parsed to a date object using us "locale" rules (month/day/year) what means inside the function we have the a date object for the 7th of january 2015.
This date then is converted to a string using the pattern you have defined.
So the outcome is as expected and "correct".
I completely agree that this behaviour is weird if you not using an us locale for your environment.
There is one simple reason for this, the date functions are pure us locale based and ignore every other locale set, simply because locale and the "LS" function was introduced later (in Macromedia CF?), so for backward compatibility they work still the classic way.
we should even consider to deprecate them for Lucee 5!

BTW in the Lucee dialect from Lucee 5 we have removed ALL this date/time function and removed the "LS" from the "LS" function, so in the Lucee dialect your code will work as expected!

Micha



On Thu, May 7, 2015 at 11:09 AM, Gavin Baumanis <gavinb...@gmail.com> wrote:
Hi Everyone,
I am having a WTF moment...

I have a form where the date is entered in AUSTRALIAN format DD/MM/YYYY

On the form it is submitted to I have the following code;
form: #form.startdate#<br />
US: #dateformat(form.startdate, "MM-DD-YYYY")#<br />
AU: #dateformat(form.startdate, "DD-MM-YYYY")#<br />
ISO: #dateformat(form.startdate, "YYYY-MM-DD")#<br />

here is the output.

form: 

US: 01-07-2015
AU: 07-01-2015
ISO: 2015-01-07

The Windows server locale is set to Australia.
The Lucee region is set to australia - and shows the date and time in Australian format correctly.

Can anyone explain what I am doing wrong?
Is there a bug?

Thanks!



--
You received this message because you are subscribed to the Google Groups "Lucee" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lucee+un...@googlegroups.com.
To post to this group, send email to lu...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages