Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Set date time based on current time

Posted on 2014-08-20
17
Medium Priority
?
151 Views
Last Modified: 2014-08-21
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>
0
Comment
Question by:earwig75
  • 9
  • 8
17 Comments
 
LVL 53

Expert Comment

by:_agx_
ID: 40274102
> 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
 
LVL 53

Expert Comment

by:_agx_
ID: 40274139
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
 

Author Comment

by:earwig75
ID: 40274154
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:earwig75
ID: 40274210
I may also need to incorporate times ilke 4:30-8:30, that's why i was using the other method.
0
 
LVL 53

Expert Comment

by:_agx_
ID: 40274228
<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
 
LVL 53

Expert Comment

by:_agx_
ID: 40274270
> 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
 

Author Comment

by:earwig75
ID: 40274447
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
 
LVL 53

Expert Comment

by:_agx_
ID: 40274514
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
 

Author Comment

by:earwig75
ID: 40274629
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
 
LVL 53

Expert Comment

by:_agx_
ID: 40274698
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
 

Author Comment

by:earwig75
ID: 40274768
I'm sorry, they are different owners. they should both be 12pm (if it's before 12pm).
0
 
LVL 53

Expert Comment

by:_agx_
ID: 40274865
> 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
 

Author Comment

by:earwig75
ID: 40275129
Sorry, I meant 12pm the next day if it's after 12 that day. I apologize I am being so confusing.
0
 
LVL 53

Expert Comment

by:_agx_
ID: 40275239
(Edit)  

Feels like we're going around in circles ;-) Did you see my last response?
0
 

Author Comment

by:earwig75
ID: 40275279
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
 
LVL 53

Accepted Solution

by:
_agx_ earned 2000 total points
ID: 40275309
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
 

Author Closing Comment

by:earwig75
ID: 40276098
You're the best.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

580 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