Just realized my calendar code is not always displaying the day 1 of the month on the correct day.
So I went back to the date handle docs and cannot determine HOW to grab the value.
I would assume I would supply CF the month and year integers and it would provide a dayofweek?
The method I am using now is clearly wrong. 😞
<cfset month_then=createdate("#year_then#","#monthis#", 01)>
<cfset month_starts_on_day=month_starts_on_day-1> forget why I added this?
<cfif month_starts_on_day is 0>
Copy link to clipboard
The arguments of createdate are integers, not strings. In any case you're on the right track.
I hope this example helps:
<cfset yearNumber=2019> <cfset monthNumber=12> <!--- Typo ---> <!--- <cfset firstDayOfMonthDate=createdate(yearNumber,monthNumber,15)>---> <!--- Correct ---> <cfset firstDayOfMonthDate=createdate(yearNumber,monthNumber,1)> <cfset weekdayNumber=dayOfWeek(firstDayOfMonthDate)> <cfset weekdayName=dayOfWeekAsString(weekdayNumber)> <cfoutput> weekdayNumber: #weekdayNumber#<br> weekdayName: #weekdayName# </cfoutput>
The output is:
Bravo! As expected you would have the correct answer.
Yet, after implementing it, I do not understand WHY the 15?
True I was using unnecessary quotes - made it look like a string.
The date in the example is
That is, the date of today. 🙂
That's not quite what I was looking for.
I was looking for the DAY of the week where 1=Sunday where
the day is the 1st of the month.
Which begs the question now WHY it is working in my code???
I just changed the 15 to a 1 in my code and am getting the exact same result.
Suppose because 15 is 1+14 and so it would still be the same day technically.
(No such thing as a coincidence in life!)
Oops, it's indeed a coincidence. There was a typo!
Thanks for spotting that. To compensate, here is a function that generalizes the idea.
<cffunction name="getFirstdayOfMonth" returntype="struct"> <cfargument name="yr" required="true"> <cfargument name="mnth" required="true"> <cfargument name="d" required="false" default="1"><!--- Optional ---> <!---Output structure containing week-day number and name of first day of month ---> <cfset var firstDayOfMonth = structNew()> <!--- First day of month ---> <cfset var firstDayOfMonthDate=createdate(arguments.yr,arguments.mnth,1)> <!--- Input date ---> <cfset firstDayOfMonth.inputDate=createdate(arguments.yr,arguments.mnth,arguments.d)> <cfset firstDayOfMonth.weekdayNumber=dayOfWeek(firstDayOfMonthDate)> <cfset firstDayOfMonth.weekdayName=dayOfWeekAsString(firstDayOfMonth.weekdayNumber)> <cfreturn firstDayOfMonth> </cffunction> <cfset firstDayOfMonth = getFirstdayOfMonth(2019,12,17)> <cfoutput> Input date: #dateFormat(firstDayOfMonth.inputDate, "dd/mm/yyyy")#<br> weekdayNumber: #firstDayOfMonth.weekdayNumber#<br> weekdayName: #firstDayOfMonth.weekdayName# </cfoutput>
Now you're just showing off! 🙂
Where are my manners?