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

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 />

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

Rendering international dates in Xamarin.Forms

I have the following XAML code:
<Label Text="{Binding startTime, StringFormat='{0:MMM d, yyyy }'}" />
which renders June 18, 2017
I'd like to create a string that renders 18 June 2017 when the culture is not US.
Any ideas on how to do that?
MMM will give you an abbreviated month name, you have to use the MMMM if you want to get the full month name. More from MSDN https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
Your XAML code should look:
<Label Text="{Binding CurrentDate, StringFormat='{0:d MMMM yyyy}'}}" />

How Do I Calculate Date Difference in Javascript

i want to count the date difference (days only) like if i am selecting "Date From" as 08-05-2016 and then "Date To" as 08-11-2016 ... it will calculate the days difference as "7" before submitting in leave count box the values on run time Using Javascript
Below is the text values codes.
Date From: <input type="date" name="from">
Date To: <input type="date" name="to">
Leave Count: <input type="text" name="leavecount">
<input type="submit" name="submit">

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>

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

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>