Search code examples
apache-flexflex4.5

Flex How to split time between day?


I would like to calculate the duration of time that span between 2 days.

Example: Date: 1/1, Start at 11pm and the duration end for 3 hours.

I would like to break the 3 hour apart for both days like the following:

Date --- Duration

Jan 1st ----- 1 hour

Jan 2nd ----- 2 hours

How to split the 3 hours time frame from Jan 1st (1hr) and the next day Jan 2nd (2hrs)?

thanks.

further explanation:

Basically, On day Jan 1st at 11pm run for 3 hrs, so the 3 hrs will span from Jan 1st to Jan 2nd. Giving Jan 1st 1hr (as Jan 1st start at 11pm [at night]) and to Jan 2nd 2hrs [in the morning].

So how to split the hours between the 2 days in code?


Solution

  • Assuming each day lasts 24 hours (no daylight savings) and that the Start is at whole hours (an integer from 0 to 23), and that the duration is also a whole number, and that the time span doesn't go beyond the second day:

    hoursInFirstDay = (start + duration > 24) ? (24 - start) : duration;
    hoursInSecondDay = duration - firstDay;
    

    For minutes, you could do something similar:

    minsInDay = 24 *60;
    
    startMoment = 60 * startHour + startMinutes;
    durationInMins = 60 * durationHours + durationMinutes;
    
    timeInFirstDay = (startMoment + durationInMins > minsInDay) ? (minsInDay - startMoment) : durationInMins;
    timeInSecondDay = durationInMins - timeInFirstDay
    
    hoursInFirstDay = int(timeInFirstDay / 60)
    minsInFirstDay = timeInFirstDay % 60
    
    hoursInSecondDay = int(timeInSecondDay / 60)
    minsInSecondDay = timeInSecondDay % 60