Search code examples
vbscriptasp-classic

Exactly minutes between two times


Need a formula that gives me the minutes between two dates. This should be used in asp classic

eg: between 08:00-20:00 I want to distribute times by 16 times. (I have 16 pieces of candy that I have to eat at exact intervals between 08:00 and 20:00)

What will be the mathematical formula for that calculation? (720 min between start and end time) I will eat a piece of candy at 08:00 and at 20:00 What will be the exact 16 minute run when I get to eat a piece of candy?

I have tried, but can't solve it, any ideas?


Solution

  • It's a bit odd that you're looking to calculate the minutes, yet in the answer you posted, you skip this calculation and instead set the answer as a predetermined variable value.

    As mentioned multiple times, DateDiff is what you need to calculate the minute intervals, along with DateAdd to calculate the 16 different times.

    Dim StartDate, EndDate, DistributionCount, Interval, x
    
    StartDate = Cdate("2023-03-27 08:00:00")
    EndDate = Cdate("2023-03-27 20:00:00")
    DistributionCount = 16
    
    Interval = Round(DateDiff("n",StartDate,EndDate)/DistributionCount)
    
    For x = 1 To DistributionCount
            
        Response.Write x & " - " & DateAdd("n",Interval*x,StartDate) & "<br>"
        
    Next
    

    Output:

    1 - 3/27/2023 8:45:00 AM
    2 - 3/27/2023 9:30:00 AM
    3 - 3/27/2023 10:15:00 AM
    4 - 3/27/2023 11:00:00 AM
    5 - 3/27/2023 11:45:00 AM
    6 - 3/27/2023 12:30:00 PM
    7 - 3/27/2023 1:15:00 PM
    8 - 3/27/2023 2:00:00 PM
    9 - 3/27/2023 2:45:00 PM
    10 - 3/27/2023 3:30:00 PM
    11 - 3/27/2023 4:15:00 PM
    12 - 3/27/2023 5:00:00 PM
    13 - 3/27/2023 5:45:00 PM
    14 - 3/27/2023 6:30:00 PM
    15 - 3/27/2023 7:15:00 PM
    16 - 3/27/2023 8:00:00 PM