Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Date comparation problem

New Here ,
Nov 01, 2011 Nov 01, 2011

I'm using datecompare tags to compare two dates.

Here's the code:

<cfset date1 = "31/10/2011 08:00:00">

<cfset date2 = "01/11/2011 08:01:01">

 

<cfif dateCompare(date1,date2, "s") EQ 1>

     date1 is later than date2

<cfelseif dateCompare(date1,date2, "s") EQ -1>

     date1 is earlier than date2

<cfelseif dateCompare(date1,date2, "s") EQ 0>

     date1 is equal to date2

</cfif>

And it need return -1 but return 1....

If change the date1 to "30/10/2011 08:00:00" it back work correct..

It's a bug with datecompare?

Thank you!

846
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 01, 2011 Nov 01, 2011

I'd start by using the createDateTime() function to create proper datetime objects rather than relying on CF guessing. Then try datecompare again.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 01, 2011 Nov 01, 2011

And it need return -1

DateCompare returns -1 when date1 is earlier than date2. That is clearly not the case with your values.

<cfset date1 = "31/10/2011 08:00:00">

That said, most CF date functions use U.S. date formatting rules when parsing date strings ie mm/dd/yyyy. So while that particular string may work, more ambiguous values like "06/10/2011" will not produce the results you expect."06/10/2011" will be interpreted as June 10th, not October 6th.  For non-US formatted date strings, use the LS (locale specific) date functions

http://help.adobe.com/en_US/ColdFusion/9.0/DevelopingWSc3ff6d0ea77859461172e0811cbec09af4-7fcf.html

http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec1a60c-7ffc.html#WSc3ff6d0ea77859461172e0811cbec22c24-6986

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 01, 2011 Nov 01, 2011

It's not a bug with datecompare, it's that you are trying to use it with strings instead of datetime objects.  The parsedatetime() function will help you.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 01, 2011 Nov 01, 2011

It's not a bug with datecompare, it's that you are trying to use it with strings instead of datetime objects. 

DateCompare does not require datetime objects. It can convert most US date strings into datetime objects automatically. The problem is their date strings are in european date format ie dd/mm/yyyy. So "01/11/2011" is being interpreted as January 11 rather than November 1.

The parsedatetime() function will help you. 

ParseDateTime also assumes US date format.  For locale specific date strings, they should use the LSParseDateTime function instead.

-Leigh

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 03, 2011 Nov 03, 2011
LATEST

Fixed using both with US dateformat without transform to lsDate =P

thank you!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources