Set date time based on current time

I need to set a date/time based on the current date/time. In the example below, if it is between 1pm and 4pm, I want to set the DueDate to the next time it is 12pm, in this case the next day. Sometimes, it may be a time in the current date; for example if it is 5am I want to set it to 12pm the same day in another cfcase. Could someone assist? Thank you.

<cfset vTime=now() />
<cfswitch expression="#datepart("h",vTime)#">
<cfcase value="13,14,15,16">
<cfset DueDate="12:00" />   <!--- I know this is incorrect, it's just here for explaining this   --->
 </cfcase>
earwig75Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_agx_Commented:
> if it is between 1pm and 4pm, I want to set the DueDate to the next time it is 12pm

What about other times?
0
_agx_Commented:
Is this what you're asking

* If the current time is 1PM or later, then dueDate = 12 noon of the next day
* Otherwise, due date =  12 noon of the current date

?


Example:
<cfset dateTimeNow = now()>

<!--- it's after 1PM return noon of the next day --->
<cfif hour(dateTimeNow) gt 13>
	<cfset dueDate = dateFormat(dateAdd("d", 1, dateTimeNow), "yyyy-mm-dd 12:00:00")>
<cfelse>
	<cfset dueDate = dateFormat(dateTimeNow, "yyyy-mm-dd 12:00:00")>
	before 1PM, return noon of today's date
</cfif>

<cfoutput>
Time now is #dateTimeNow#<br>
due date = #parseDateTime(dueDate)#<br>
</cfoutput>

Open in new window

0
earwig75Author Commented:
I will have a few different due dates based on the current time. So for instance:

between 9am-4pm... due date 8pm same day
between 4pm-12am.. due date 12pm next day
between 12am-9am due date 12pm same day
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

earwig75Author Commented:
I may also need to incorporate times ilke 4:30-8:30, that's why i was using the other method.
0
_agx_Commented:
<cfset dateTimeNow = now()>
<cfset hourNow = hour(dateTimeNow)>

<!--- between 9am-4pm... due date 8pm same day --->
<cfif hourNow gte 9 and hourNow lt 16>
	<cfset dueDate = parseDateTime(dateFormat(dateTimeNow, "yyyy-mm-dd") &" 20:00:00")>

<!--- between 4pm-12am.. due date 12pm next day --->
<cfelseif hourNow gte 16 and hourNow lte 23>
	<cfset dueDate = parseDateTime(dateFormat(dateAdd("d", 1, dateTimeNow), "yyyy-mm-dd") &" 12:00:00")>

<!--- between 12am-9am due date 12pm same day --->
<cfelse>
	<cfset dueDate = parseDateTime(dateFormat(dateTimeNow, "yyyy-mm-dd") &" 12:00:00")>
</cfif>

<cfoutput>
hour now is #timeFormat(dateTimeNow, "h:mm tt")#<br>
due date = #dateFormat(dueDate, "mm-dd-yyy")# #timeFormat(dueDate, "h:mm tt")#<br>
</cfoutput>

Open in new window

0
_agx_Commented:
> I may also need to incorporate times ilke 4:30-8:30, that's why i was using the other method.

Hm... that's a little different than what you described earlier, and none of the examples would account for minutes.  What's the actual requirement? Can these time ranges span days... ie For example 4:30PM-12:30AM ... would span multiple days.
0
earwig75Author Commented:
I'm sorry I didn't mention the 1/2 hours. Yes sometimes they will span days like you described. I can live without the 1/2 hours for now but sometimes it will span a day.
0
_agx_Commented:
It could certainly be done, but the exact code depends on exactly what the ranges are and whether they're static or dynamic (harder). Are you always checking for the same 3 ranges? If so, what are the ranges ... ?

     <!--- actual times might include 1/2 hours --->
      between 9am-4pm ... due date 8pm same day
      between 4pm-12am.. due date 12pm next day
      between 12am-9am due date 12pm same day

If not, what are the ranges and rules?
0
earwig75Author Commented:
these are the times but i am not 100% sure of the due date time... but it will always be the next time that "time" is reached.

12am-8:30am due date 12pm (same day)

6am-2:30pm due date 4pm (same day)

7am-3:30pm due date 12pm (next day)

3:00pm-11:30pm due date 12pm (next day)

4:00pm-12:30am due date 12pm (same day as 12:30am end time)
0
_agx_Commented:
Hm... those ranges don't really make sense. Some of the times overlap, so there could be two different results.  For example

12am-8:30am due date 12pm (same day)
  6am-2:30pm due date 4pm (same day)
0
earwig75Author Commented:
I'm sorry, they are different owners. they should both be 12pm (if it's before 12pm).
0
_agx_Commented:
> they should both be 12pm (if it's before 12pm).

It's still not making sense. 2:30pm is after 12pm of the same day. Plus, that's not the only range that overlaps.

Seems like you're omitting an important piece of information.

I don't know what an "owner" is in this context or how it relates to the ranges.  But it seems like you're trying to write a single piece of logic to handle multiple sets of time ranges. That can't possibly work reliably if the times overlap, because the same time could yield different results.

For example, let's use a 3 shift work day as an example.  If you have different client schedules:

         Client A uses this schedule
         -------
         8am - 4pm    -> due date is xxxxx
         4pm - 12am  -> due date is xxxxx
         12am - 8am  -> due date is xxxxx

         Client B uses this schedule
         -------
         7:30am - 3:30pm    -> due date is xxxxx
         3:30pm - 11:30pm   -> due date is xxxxx
         11:30pm - 7:00am  -> due date is xxxxx


You'll need different logic depending on which client you're handling

       <cfif client is "client A">
              use first logic rules
       <cfelseif client is "client B">
             use alternate logic...
       </cfif>

So how many "clients" (ie owners, whatever...) are there?
What are the schedules (ie time ranges) for each client?
0
earwig75Author Commented:
Sorry, I meant 12pm the next day if it's after 12 that day. I apologize I am being so confusing.
0
_agx_Commented:
(Edit)  

Feels like we're going around in circles ;-) Did you see my last response?
0
earwig75Author Commented:
I think I'm making this more complicated than it needs to be.  As long as it goes to noon the next day if it's already past noon the current day I can make it work. :)
0
_agx_Commented:
Haha, could be.  If that's all you need, just go back to the original example and use 12 instead of 1PM:


<cfset dateTimeNow = now()>
<!--- it's noon or later, go to next day ... --->
<cfif hour(dateTimeNow) gte 12>
      <cfset dueDate = dateFormat(dateAdd("d", 1, dateTimeNow), "yyyy-mm-dd")&" 12:00:00">
<cfelse>
      <cfset dueDate = dateFormat(dateTimeNow, "yyyy-mm-dd")&" 12:00:00">
</cfif>

<cfdump var="#parsedateTime(dueDate)#">
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
earwig75Author Commented:
You're the best.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ColdFusion Language

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.