Search code examples
coldfusion

Dateformat ww returns wrong week of year


Can someone explain why getting the week of this date returns 1 instead of 53? And how do I get coldfusion to return 53 for this date?

<cfscript>
    Date1 = "{ts '2019-12-30  12:00:00'}";
    dateformat= DateFormat(Date1)
    week= DateFormat(Date1,"ww")
    writeOutput("Week: " & week & "<br/>")
</cfscript>

See the coldfusion fiddle: https://cffiddle.org/app/file?filepath=ab0d50c0-d268-4769-b5eb-a16a8fca985a/dc6aebb2-76a8-4485-a5eb-e9eb17b293cb/9e6e8b2c-f570-45be-88a1-49db39f8c69b.cfm


Solution

  • You can use week(Date) instead of trying dateformat.

    I am not sure why dateformat is taking the last week of 2019 as the first week of 2020. Most likely dateformat is not designed to handles these nuanced logics. Since Week function is must be specifically designed for this purpose it would be wise to make use of that. I'll update the answer if I can find anything specific to suggest the reason for dateformat issue.

    Update from comment by @Agreax :

    Week() uses Adobe's internal logic. DateFormat() uses java's SimpleDateFormat class which ".. depends on the values of getMinimalDaysInFirstWeek(), getFirstDayOfWeek(), and the day of the week of January 1..". With the default settings, 12/30/2019 is considered part of the 1st week of 2020.