ColdFusion Calendar - How can I find day from days in month? - date

Here's my code. I got it from a tutorial online.
<CFPARAM NAME = "month" DEFAULT = "#DatePart('m', Now())#" />
<CFPARAM NAME = "year" DEFAULT = "#DatePart('yyyy', Now())#" />
<CFPARAM NAME = "currentday" DEFAULT = "#DatePart('d', Now())#" />
<CFPARAM NAME = "startmonth" DEFAULT = "#DatePart('m', Now())#" />
<CFPARAM NAME = "startyear" DEFAULT = "#DatePart('yyyy', Now())#" />
<cfset ThisMonthYear = CreateDate(year, month, '1') />
<cfset Days = DaysInMonth(ThisMonthYear) />
<cfset LastMonthYear = DateAdd('m', -1, ThisMonthYear) />
<cfset LastMonth = DatePart('m', LastMonthYear) />
<cfset LastYear = DatePart('yyyy', LastMonthYear) />
<cfset NextMonthYear = DateAdd('m', 1, ThisMonthYear) />
<cfset NextMonth = DatePart('m', NextMonthYear) />
<cfset NextYear = DatePart('yyyy', NextMonthYear) />
and here is my output code.
<a href="calendar_day.cfm?month=#month#&day=#THE_DAY#&year=#year#">
I'm using this for a visible calendar, and want to be able to select the day from all days in the month. Is there any way to determine the day of the month when clicking on the day in the monthly calendar view?

I believe what you want to use is #URL.day#, to get the day variable passed in the URL, but as everyone is saying your question is really confusing.

As pointed out, the question is pretty confusing.
Is there any way to determine the day of the month when clicking on the day in the monthly calendar view?
You have total number of days in the month in Days var. On the calendar render, you are likely looping through and displaying each day of the month using that var. You can easily embed that loop index into your resulting HTML to know what day any given link would refer to.
<!--- loop thru all days in current month --->
<cfloop from=1 to=Days index="this_day">
<!--- display day in the calendar --->
Day #this_day#
<cfif this_day eq CurrentDay>
<!--- day being displayed is the current day, highlight it or whatever --->
</cfif>
</cfloop>

Related

Get Number of Occurrences Based on Date Range and Set Days of Week

I am trying to calculate the total number of events that would occur based on the following known parameters
start date
end date
Days of the week
frequency of weeks (every week, every 2 weeks, etc)
For example, based on the following example data:
05/01/2017
05/31/2017
1,4 (Sunday, Wednesday)
Every 2 weeks
I would have to calculate that this event would run four times (5/7, 5/10, 5/21, 5/24)
Below is the skeleton I have set up. I'm fully stumped on how to increment the current day in the loop based on the number of weeks have passed.
<cfset local.totalRuns = 0 />
<cfset local.startDate = '2017-05-01' />
<cfset local.endDate = '2017-05-31' />
<cfset local.totalDays = DateDiff("d", local.startDate, local.endDate) />
<cfset local.daysPerWeek = '1,4' />
<cfset local.recurrence = 2 />
<cfset local.currentLoop = 0 />
<cfset local.weeksToCount = local.recurrence * 2 />
<!--- Loop a day at a time --->
<cfloop from="#local.startDate#" to="#local.endDate#" index="local.thisDay" step="#createTimespan(1, 0, 0, 0)#">
<cfset local.currentDate = local.thisDay />
<!--- Loop over each allowed day of the current week and determine actual date --->
<cfloop list="#local.daysPerWeek#" index="local.currentDay">
<!--- if current date does not exceed the end date add increment (this current is incorrect) --->
<cfif DateDiff("d", local.currentDate, local.endDate) LTE 0>
<cfset local.totalRuns++ />
</cfif>
</cfloop>
<cfset local.currentLoop++ />
</cfloop>
This is a formatted comment. It shows the approach I would take.
set the recurrence count to 0
start looping through the list of valid days of the week (1,4) in this case
set the control date to the start date.
do something to set the control date to the earliest
date that matches the day of the week in this loop.
if the control date is greater than the end date, break out of the loop.
otherwise
add 1 to the recurrence count
set up a while loop that adds the specified number of weeks
to the control date, and repeats the check I just described
end looping through the list of valid days of the week (1,4) in this case

Watir,click on a date from a date picker 62 days from current date

I need to click on a date that is 62 days later than the current date.
The next button for the months is clickable so I have been able to move the calendar to 2 months later but unable to select the date.
Eg if the current date is 4 July then I need to select 6th Sept.
My code at the moment looks somewhat like this.
b.element(:name, "policyperiod_endDate").click <-- datetime picker's click
b.element(:xpath, "(//button[#type='button'])[50]").click <-- next month
b.element(:xpath, "(//button[#type='button'])[50]").click
b.element(:link,"06").click <--doesnt select 08
b.element(:xpath, "(//button[#type='button'])[68]").click <-selects fixed cell n
how to select 06?
The outer HTML is like this:
input type="text" class="pull-left pdr35 ng-isolate-scope ng-pristine ng- invalid ng-invalid-required" datepicker-popup="yyyy-MM-dd" is-open="datePickerOpts.datepickers.policyperiod_startdate[1]" ng-model="policyIVOs.policyEndDate" name="**policyperiod_endDate**" datepicker-options="dateOptions" show-button-bar="false" close-on-date-selection="true" show-weeks="false" ng-click="open($event, 'policyperiod_startdate', 1)" min-date="policyEndMin" max-date="policyEndMax" required="" readonly=""

Get date based on month, year, day and week number (ColdFusion)

I found a similar answer to my question, but it was in SQL and kinda went over my head (Given year, month, day and week number how to find the date?(Sql Server 2005 Set based)).
I'm writing a schedule creation app where a user will select the month, day of week (Sun-Sat, aka 1-7), and choose optionally if this will recur throughout the month each week.
Given that, I'm trying to write a function in ColdFusion that will return the actual date if I pass in the month, day, week number, and year. Dates always confuse the heck out of me.
So I know this is an old post but here are some current options in script or tag.
Week Number<br />
<cfscript>
myDateTime = now();
WriteOutput(week(myDateTime & "<br/>"));
</cfscript>
<br />
Day of week<br />
<cfscript>
mydayofweek = (DayOfWeek(myDateTime));
WriteOutput(DayOfWeek(myDateTime & "<br/>"));
</cfscript>
<br />
Day of year<br />
<cfscript>
WriteOutput(DayOfYear(myDateTime & "<br/>"));
</cfscript>
<br />
Days in month<br />
<cfscript>
WriteOutput(DaysInMonth(myDateTime & "<br/>"));
</cfscript>
<br />
<!--- What date are we working with --->
<cfset workingdt = '2022-5-05' />
<!--- Get the week start date. --->
<cfset dtWeekStart = (workingdt - DayOfWeek( workingdt ) + 1) />
<!--- Get the week end date. --->
<cfset dtWeekEnd = (workingdt + (7 - DayOfWeek( workingdt ))) />
<!--- Get the working week --->
<cfset dtworkingweek = '#week(workingdt)#' />
<br />
<!--- Output the dates: --->
<cfoutput>
Today: #DateFormat( workingdt )#<br />
Week Start: #DateFormat( dtWeekStart )#<br />
Week End: #DateFormat( dtWeekEnd )#<br />
Week Number: #dtworkingweek#<br />
</cfoutput>
<br />

ColdFusion createdate()

I am just learning about ColdFusion function CreateDate. But, when I use CreateDate the value output is different. I mean it changes month to day and day to month.
<cfoutput>
<cfset txtBirthDate='07-10-1983'>
<cfset valueOf_txtBirthDate = dateFormat(CreateDate(Year(txtBirthDate),Month(txtBirthDate),Day(txtBirthDate)),'YYYY-MMM-DD')>
#txtBirthDate#<br/><br/>
#valueOf_txtBirthDate#<br/>
</cfoutput>
The value of txtBirthDate is 07-10-1983, but the value of valueOf_txtBirthDate that CreateDate created is 1983-Jul-10. Why is it July? It is supposed to be October: 07(date), 10(month), 1983(year).
Is there something wrong with the format?
You are using createDate() in a very convoluted way. txtBirthDate isn't a date, so you shouldn't use it as an input for date functions like year(), month() etc. The input should already be a date object by the time you use a date function.
Let's say you start with the string 07-10-1983, where the format is mm-dd-yyyy.
txtBirthDate ='07-10-1983'; // dd-mm-yyyy
// extract the specific date parts from the string
yyyy = listLast(txtBirthDate, "-");
mm = listGetAt(txtBirthDate, 2, "-");
dd = listFirst(txtBirthDate, "-");
// create a date object out of those components
birthDate = createDate(yyyy, mm, dd);
// output the date object in human readable format
writeOutput(dateFormat(birthDate, "YYYY-MM-DD"));
(obviously in real code you'd not have those comments which serve only to state the obvious!)
Only ever use dateFormat() at the last point as you output it. For other date operations, use the actual date object: birthDate.
Because by default your date is mm/dd/yyyy format. So
<cfset txtBirthDate='07-10-1983'>
will be read as July 10th, 1983 by CF. Hence the outout...
You can use Java function to perform this.
This is how it works:
<cfset txtBirthDate='07-10-1983' />
<cfset formatter = createObject("java","java.text.SimpleDateFormat") />
<cfset formatter.init("dd-MM-yyyy") />
<cfset birthDate= formatter.parse(txtBirthDate) />
it seems like should do the trick:
<cfset txtBirthDate = '10-7-1983'>
#DateFormat(txtBirthDate, "MMM, D, YYYY")#
notice i reversed the position of month and day in cfset to make it come out right.
you can do something simple like that :
txtBirthDate = '07-10-1983'; //dd-mm-yyyy
arrayDate = listToArray(txtBirthDate, '-');
date_birthDate = createDate(arrayDate[3], arrayDate[2], arrayDate[1]);
writeOutput(lsDateFormat(date_birthDate, 'yyyy-mm-dd');
<cfscript>
txtBirthDate=CreateODBCDate("1983-10-07");
txtBirthDate=LSDateFormat(txtBirthDate, "dd/mm/yyyy");
</cfscript>
<cfoutput>
#txtBirthDate#
</cfoutput>

Modifying date in ColdFusion

How can I adjust a date in coldfusion to get the next day at 1AM?
the date is taken from a database, and stored as a string. I'm thinking the way to do it is through CreateDateTime and filling it with the time and date using year,month,day + 1 etc.
I'm just worried that it won't work when the next day falls on the next month
Using DateAdd() you can always be sure that it will take the context of the current date into account. So if it is August 31 and you add one day it will correctly make the date Sept 1st. It will also properly switch the year if you did the same on Dec 31st.
<cfset nextDate = dateAdd("d", 1, now()) />
<cfset nextDateWithTime = createDateTime(year(nextDate), month(nextDate), day(nextDate), 1, 0, 0) />
<cfoutput>#nextDateWithTime#</cfoutput>
Assuming the date is something which CF recognizes as a date, and contains date only, without time, you could do something like:
<cfscript>
function tomorrowOneAM(date) {
var resultValue = DateAdd("d",1,date);
resultValue = DateAdd("h",1,resultValue);
return resultValue;
}
</cfscript>