Skip to main content
Known Participant
December 14, 2019
Answered

Determining the day of the first day of a month

  • December 14, 2019
  • 1 reply
  • 1076 views

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?

Or not?

The method I am using now is clearly wrong. 😞

<cfset month_then=createdate("#year_then#","#monthis#", 01)>

<cfset month_starts_on_day=dayofweek(month_then)>
<cfset month_starts_on_day=month_starts_on_day-1>  forget why I added this?
<cfif month_starts_on_day is 0>
             <cfset month_starts_on_day=7>
</cfif>


<cfset dim=daysinmonth("#month_then#")>
<cfset dim_total=dim>
<cfset dim=dim+month_starts_on_day>

This topic has been closed for replies.
Correct answer BKBK

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:

    weekdayNumber: 1
    weekdayName: Sunday 

1 reply

BKBK
Community Expert
BKBKCommunity ExpertCorrect answer
Community Expert
December 15, 2019

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:

    weekdayNumber: 1
    weekdayName: Sunday 

Known Participant
December 15, 2019

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. 

 

BKBK
Community Expert
Community Expert
December 15, 2019

The date in the example is

 

Year: 2019

Month: 12

Day: 15

 

That is, the date of today. 🙂