Solved

DateTime question

Posted on 2014-09-22
5
140 Views
Last Modified: 2014-09-25
I am trying to create a condition for scheduling interface. But there is also a rule behinf it .

Once a session is scheduled, students may change or cancel up to five (5) calendar days before the scheduled session. The five days are worked out to the minute the session starts. Once the student is inside the five (5) calendar day window (up to the minute) prior to a scheduled time, the session cannot be rescheduled or cancelled. Please be sure that the selected sessions are sessions that will be attended by the student

How would I implement that
0
Comment
Question by:erikTsomik
  • 3
  • 2
5 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
Comment Utility
Take the scheduled date and time and subtract 5 days to get the cutoff date. Then compare the current date and time now to the cutoff date, down to the minutes. If it's later (ie they're within the 5 day window) don't allow any changes.

<cfset cutoffDateTime = dateAdd("d", -5, theScheduledDateTime)>
<cfif dateCompare(now(), cutoffDateTime , "n") gte 0>
	inside five day window. no changes allowed
<cfelse>
	changes allowed. before five day window. 
</cfif>

Open in new window

0
 
LVL 19

Author Closing Comment

by:erikTsomik
Comment Utility
is DateCompare is a good command to use. is DateDiff is better?
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
They do different things, so .. it all depends on the task. In this case, DateCompare is better IMO. You don't care about the number of minutes difference ... just whether or not the current date and time is before the cutoff date.
0
 
LVL 19

Author Comment

by:erikTsomik
Comment Utility
well in my case I do care about the time portion
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
Right. What I meant was, while you could do the same thing with DateDiff, it it's more complicated than necessary. Since you do care about the time portion, you'd have to calculate the number of minutes (or seconds) difference between the two dates:

            minutesDiff = dateDiff( date1, date2, "n");

Then use a formula to figure out how many minutes are in 5 days

      ie minutesIn5Days = 60 min per hour * 24 hours per day * 5 days.  

Then determine if #minutesDiff# is less than #minutesIn5Days#

Why do all that conversion logic, when you do not really care exactly how many minutes are in 5 days... You just want to know if the current date falls within the window or not.  The conversion stuff just makes the code more complicated IMO.  

Since the business rule is within 5 days, using dateAdd("d[ays]", -5, ..)" is easier to follow than a bunch of code converting time units. This does the same thing, more intuitively IMO:

     <cfset cutoffDateTime = dateAdd("d", -5, theScheduledDateTime)>
     <cfif dateCompare(now(), cutoffDateTime , "n") gte 0>
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now