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

Determining the day of the first day of a month

Community Beginner ,
Dec 14, 2019 Dec 14, 2019

Copy link to clipboard

Copied

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>

TOPICS
Advanced techniques , Documentation

Views

769

Translate

Translate

Report

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

correct answers 1 Correct answer

Community Expert , Dec 15, 2019 Dec 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
...

Votes

Translate

Translate
Community Expert ,
Dec 15, 2019 Dec 15, 2019

Copy link to clipboard

Copied

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 

Votes

Translate

Translate

Report

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
Community Beginner ,
Dec 15, 2019 Dec 15, 2019

Copy link to clipboard

Copied

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. 

 

Votes

Translate

Translate

Report

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
Community Expert ,
Dec 15, 2019 Dec 15, 2019

Copy link to clipboard

Copied

The date in the example is

 

Year: 2019

Month: 12

Day: 15

 

That is, the date of today. 🙂

Votes

Translate

Translate

Report

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
Community Beginner ,
Dec 15, 2019 Dec 15, 2019

Copy link to clipboard

Copied

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. 

Coincidence. 🙂

(No such thing as a coincidence in life!)

 

 

 

Votes

Translate

Translate

Report

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
Community Expert ,
Dec 15, 2019 Dec 15, 2019

Copy link to clipboard

Copied

Oops, it's indeed a coincidence. There was a typo! 

Correct is 

 

<cfset firstDayOfMonthDate=createdate(yearNumber,monthNumber,1)>

 

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>

 

 

 

 

 

 

 

Votes

Translate

Translate

Report

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
Community Beginner ,
Dec 15, 2019 Dec 15, 2019

Copy link to clipboard

Copied

LATEST

Now you're just showing off!  🙂

 

Where are my manners?

 

Cheers ... 

 

 

Votes

Translate

Translate

Report

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
Documentation