Jump to content

Add time To anothe time in an interval


Recommended Posts

Hi everybody,

I want to do a script to calculate the date/time in an interval.

The interval is 9h-12h and 14h-19h. So When I want to add 3 hours of "2011/01/05 17:00:00" it must be "2011/02/05 10:00:00" and not "2011/01/05 20:00:00"

I think to use _DateDiff() and _DateAdd(), but don't say how proceed to do this.

Thanks in advance

Qui ose gagneWho Dares Win[left]CyberExploit[/left]

Link to comment
Share on other sites

What's the maximum hours you need to add?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

9-12 and 14-19

if you added 3 to 17 you would be at 9 not at 10, no?

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Something like this should work:

#include <date.au3>

$sStartDateTime = "2011/01/05 11:00:00"
$iMinutesToAdd = 120 ; 2 hours
$sStartRange1 = "09:00:00"
$sEndRange1   = "12:00:00"
$sStartRange2 = "14:00:00"
$sEndRange2   = "17:00:00"

$sEndDateTime = _DateAdd("n", $iMinutesToAdd, $sStartDateTime)
If StringMid($sStartDateTime,12) >= $sStartRange1 And StringMid($sStartDateTime,12) <= $sEndRange1 Then   ; StartDateTime is in Range 1
    If StringMid($sEndDateTime,12) > $sEndRange1 Then $sEndDateTime = _DateAdd("h", 2, $sEndDateTime)   ; EndDateTime exceeds Range 1 => add 2 hours
Else
    If StringMid($sEndDateTime,12) > $sEndRange2 Then $sEndDateTime = _DateAdd("h", 16, $sEndDateTime)  ; EndDateTime exceeds Range 2 => add 16 hours
EndIf
ConsoleWrite($sEndDateTime & @CRLF)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Something like this should work:

#include <date.au3>

$sStartDateTime = "2011/01/05 11:00:00"
$iMinutesToAdd = 120 ; 2 hours
$sStartRange1 = "09:00:00"
$sEndRange1   = "12:00:00"
$sStartRange2 = "14:00:00"
$sEndRange2   = "17:00:00"

$sEndDateTime = _DateAdd("n", $iMinutesToAdd, $sStartDateTime)
If StringMid($sStartDateTime,12) >= $sStartRange1 And StringMid($sStartDateTime,12) <= $sEndRange1 Then   ; StartDateTime is in Range 1
    If StringMid($sEndDateTime,12) > $sEndRange1 Then $sEndDateTime = _DateAdd("h", 2, $sEndDateTime)   ; EndDateTime exceeds Range 1 => add 2 hours
Else
    If StringMid($sEndDateTime,12) > $sEndRange2 Then $sEndDateTime = _DateAdd("h", 16, $sEndDateTime)  ; EndDateTime exceeds Range 2 => add 16 hours
EndIf
ConsoleWrite($sEndDateTime & @CRLF)

When I put :

$sStartDateTime = "2011/01/05 18:30:00"

with 2 hours :

$iMinutesToAdd = 120 ; 2 hours

the return value is 2011/01/06 12:30:00 !

and the result should be 2011/01/06 10:00:00

Edited by jerem488

Qui ose gagneWho Dares Win[left]CyberExploit[/left]

Link to comment
Share on other sites

Not 9 but 10 because 17 to 19 => 2 hours and 09h to 10h do 3hours

if 17 + 1 = 18 and 17 + 2 = 19 and 17 + 3 = 10 Then there is no such animal as 19:30 (because 19 and 9 are essentially the same number) so:

18:30:00

with 2 hours :

$iMinutesToAdd = 120 ; 2 hours

and the result should be 2011/01/06 09:30:00

18:30 + 1 = 9:30 and 18:30 + 2 = 10:30

Once again I question the math and you may want to consider ending at 18:59:59 to avoid such confusion, which admittedly may only be mine.

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

if 17 + 1 = 18 and 17 + 2 = 19 and 17 + 3 = 10 Then there is no such animal as 19:30 (because 19 and 9 are essentially the same number) so:

18:30 + 1 = 9:30 and 18:30 + 2 = 10:30

Once again I question the math and you may want to consider ending at 18:59:59 to avoid such confusion, which admittedly may only be mine.

Yes that's it ! So there has an error in the script. And I try to understand the error, but I don't see this error

Qui ose gagneWho Dares Win[left]CyberExploit[/left]

Link to comment
Share on other sites

This may be closer, just need to check the return from the _DateAdd (and decide what you want to do with those), as times like 23:30 would return 13:30 which is still out of range.

#include <date.au3>

$sStartDateTime = "2011/01/05 19:30:00"
$sStartRange1 = "09:00:00"
$sEndRange1   = "11:59:59"
$sStartRange2 = "14:00:00"
$sEndRange2   = "18:59:59"



    If StringMid($sStartDateTime,12) > $sEndRange1 And StringMid($sStartDateTime,12) < $sStartRange2 Then
    $sEndDateTime = _DateAdd("h", 2, $sStartDateTime)   ; EndDateTime exceeds Range 1 => add 2 hours
ElseIf StringMid($sStartDateTime,12) > $sEndRange2 Then
    $sEndDateTime = _DateAdd("h", 14, $sStartDateTime)  ; EndDateTime exceeds Range 2 => add 16 hours
Else
    $sEndDateTime = $sStartDateTime
EndIf

ConsoleWrite($sEndDateTime & @CRLF)
Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

This may be closer, just need to check the return from the _DateAdd (and decide what you want to do with those), as times like 23:30 would return 13:30 which is still out of range.

#include <date.au3>

$sStartDateTime = "2011/01/05 19:30:00"
$sStartRange1 = "09:00:00"
$sEndRange1   = "11:59:59"
$sStartRange2 = "14:00:00"
$sEndRange2   = "18:59:59"



    If StringMid($sStartDateTime,12) > $sEndRange1 And StringMid($sStartDateTime,12) < $sStartRange2 Then
    $sEndDateTime = _DateAdd("h", 2, $sStartDateTime)   ; EndDateTime exceeds Range 1 => add 2 hours
ElseIf StringMid($sStartDateTime,12) > $sEndRange2 Then
    $sEndDateTime = _DateAdd("h", 14, $sStartDateTime)  ; EndDateTime exceeds Range 2 => add 16 hours
Else
    $sEndDateTime = $sStartDateTime
EndIf

ConsoleWrite($sEndDateTime & @CRLF)

This don't work, and we can't specify time to add.

Like this it work :

#include <date.au3>

$sStartDateTime = "2011/01/05 16:30:00"
$iMinutesToAdd = 35
$sStartRange1 = "09:00:00"
$sEndRange1   = "12:00:00"
$sStartRange2 = "14:00:00"
$sEndRange2   = "17:00:00"

$sEndDateTime = _DateAdd("n", $iMinutesToAdd, $sStartDateTime)
If StringMid($sStartDateTime, 12) >= $sStartRange1 And StringMid($sStartDateTime, 12) <= $sEndRange1 Then   ; StartDateTime is in Range 1
    If StringMid($sEndDateTime,12) > $sEndRange1 Then $sEndDateTime = _DateAdd("h", 2, $sEndDateTime)   ; EndDateTime exceeds Range 1 => add 2 hours
    ConsoleWrite($sEndDateTime & " rr" & @CRLF)
ElseIf StringMid($sStartDateTime, 12) >= $sStartRange2 And StringMid($sStartDateTime, 12) <= $sEndRange2 Then   ; StartDateTime is in Range 1
    If StringMid($sEndDateTime,12) > $sEndRange2 Then $sEndDateTime = _DateAdd("h", 16, $sEndDateTime)   ; EndDateTime exceeds Range 1 => add 2 hours
    ConsoleWrite($sEndDateTime & " rr" & @CRLF)
EndIf
Edited by jerem488

Qui ose gagneWho Dares Win[left]CyberExploit[/left]

Link to comment
Share on other sites

Was just trying to simply show the math and how the _DateAdd return could be out of the established range. I would certainly use water's function if you need to set the hours dynamically, and you would still need to handle returns from the _DateAdd unless you find a cooler way to do it.

all apologies if I gave the impression I was solving all the issues at once.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

This code should handle the situation that $sStartDateTime is not within one of the ranges.

#include <date.au3>

$sStartDateTime = "2011/01/05 07:00:00"
$iMinutesToAdd = 120 ; 2 hours
$sStartRange1 = "09:00:00"
$sEndRange1 = "12:00:00"
$sStartRange2 = "14:00:00"
$sEndRange2 = "17:00:00"

; If $sStartDateTime is not within a range move $sStartDateTime to the start of the next range
$sStartTime = StringMid($sStartDateTime, 12)
If $sStartTime > $sEndRange1 And $sStartTime < $sStartRange2 Then $sStartTime = $sStartRange2  ; Time between Range 1 and Range 2 => move to begin of Range 2
If $sStartTime > $sEndRange2 Or $sStartTime < $sStartRange1 Then $sStartTime = $sStartRange1   ; Time between Range 2 and Range 1 => move to begin of Range 1
$sStartDateTime = StringLeft($sStartDateTime, 11) & $sStartTime
$sEndDateTime = _DateAdd("n", $iMinutesToAdd, $sStartDateTime)
If StringMid($sStartDateTime, 12) >= $sStartRange1 And StringMid($sStartDateTime, 12) <= $sEndRange1 Then ; StartDateTime is in Range 1
    If StringMid($sEndDateTime, 12) > $sEndRange1 Then $sEndDateTime = _DateAdd("h", 2, $sEndDateTime) ; EndDateTime exceeds Range 1 => add 2 hours
Else
    If StringMid($sEndDateTime, 12) > $sEndRange2 Then $sEndDateTime = _DateAdd("h", 16, $sEndDateTime) ; EndDateTime exceeds Range 2 => add 16 hours
EndIf
ConsoleWrite($sEndDateTime & @CRLF)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Hi Guys,

Pardon my intrusion but am too stuborn to leave this alone without understanding it!

How does this work "if 17 + 1 = 18 and 17 + 2 = 19 and 17 + 3 = 10"

Obviously I have failed to get the point of this thread, please enlighten (then maybe headache will go away).

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

This is how I understand this thread:

The OP has two intervals (morning and afternoon). He has a point in time that can be between 00:00 and 24:00. He want's to add a number of minutes and the calculated time has to be within one of the intervals.

Example: If you have an helpdesk and a user reports an incident the helpdesk has to act within 2 hours during business time. So he wants to calculate when the SLA is violated.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

if you have two ranges (9, 10 , 11 , 12) (14, 15 , 16 , 17 , 18 , 19) and you have 17 + 3 you would be back at 9.

However the OP intended the clock to end at 18:59, such that there really is no 19 in the second range as the clock would roll to 9. which is why he replied

Not 9 but 10 because 17 to 19 => 2 hours and 09h to 10h do 3hours

To my first math question.

Hope that helps.

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...