TransWikia.com

Generate Time ranges in Excel 365

Super User Asked by spydercoll on January 17, 2021

What is the formula for calculating a random time that ends later than the start time calculated in another cell?

For example, A1 contains the formula =RANDBETWEEN(1,1359)/60 formatted as a time, so I get a start time between 00:01 and 23:59. I need a formula that will give me an end time that is later than the time in A1.

One Answer

For example, A1 contains the formula =RANDBETWEEN(1,1359)/60 formatted as a time, so I get a start time between 00:01 and 23:59. I need a formula that will give me an end time that is later than the time in A1.

That does not work. Excel stores date and time information as floating point numbers. As opposed to integers, floating point can contain numbers behind the decimal comma.

Each day counts as 1 before the comma, hours, minutes and seconds are calculated are fractions. One hour would be represented as 1/24. One minute would be represented by 1/24/60.

A day has 24*60= 1440 minutes. "23:59" is just a minute less so that is minute number 1439. Therefore your formula would rather be RANDBETWEEN(1,1439). That gives you a random minute between minute number 1 and minute number 1439. To convert that into a fraction corresponding to Excel date and time representation, divide it by the total number of minutes in a day RANDBETWEEN(1,1439)/24/60.

Given your start time in minutes A1=RANDBETWEEN(1,1439) you might calculate the end time as an offset to the start time: A2=RANDBETWEEN(A1,1439)+A1 That will give you a start and end minute of the day. To convert those minutes into Excel date/time format you write: B1=A1/24/60 B2=A2/24/60 and format both as times.

P.S: If you want to be 100% bullet-proof to be compliant with the future version "Excel 2120" you would not use the division above, Microsoft might perhaps change its representation of date and time. Instead, you would use the Excel time()-function to convert hours, minutes and seconds into a time expression. Combined with your restriction for the second value the calculation would get a little bit more complicated. :)

For infotainment about date and time representation please read:

https://www.joelonsoftware.com/2006/06/16/my-first-billg-review/

Answered by r2d3 on January 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP