I need to round date and time up and down to the hour in Excel.
The MROUND formula only does it to the nearest hour, but in the case of 10/05/2016 09:43 I want it to be rounded down to 10/05/2016 9:00 and in some cases I want to round up too.
It's essential that they stay in the format dd/mm/yyyy hh:mm:ss
You can do it this way:
=ROUNDDOWN(A1*24,0)/24
=ROUNDUP(A1*24,0)/24
Then you can set any datetime format you need using Cell/Format.
Related
I have 2 datetime strings in ISO 8601 format e.g. 1900-01-01 00:00:00+00:00 and 2020-03-27 23:59:59+11:00.
How can I calculate the number of seconds between these 2 datetime strings using only Excel or Excel VBA?
All the answers I come across calculates the date part only, eventhough the questions asked for timestamp.
Function TOUNIX(dt) As Long
TOUNIX = DateDiff("s", "1/1/1970", dt)
End Function
Broken down for understanding/educational purposes:
LEFT(A1,FIND("+",A1)-1)-1 - extracts the date
(VALUE(LEFT(A1,FIND("+",A1)-1)-1) - converts date to a number (-1 to account for the first second being 0)
(LEFT(MID(A1,FIND("+",A1)+1,5),2) - extract the hour from the timezone
RIGHT(MID(A1,FIND("+",A1)+1,5),2) - extracts the minutes from the timezone
/60 - 60 minutes in an hour
/24 - 24 hours in a day
IF(LEFT(RIGHT(A1,6),1)="-",1,-1) - extract the timezone modifier to determine whether the timezone adds or subtracts from value
Combined to this point, we have the number converted value of the date
* 86400 - The amount of seconds in a day
=(VALUE(LEFT(A1,FIND("+",A1)-1)-1)+((LEFT(MID(A1,FIND("+",A1)+1,5),2)+(RIGHT(MID(A1,FIND("+",A1)+1,5),2)/60))/24)*IF(LEFT(RIGHT(A1,6),1)="-",1,-1))*86400
Where you are starting with 0, you won't need to subtract from another date. If you wanted to anyway, just duplicate the formula as needed.
you will want to look at the following functions.
For text manipulations:
LEFT
RIGHT
MID
FIND
SEARCH
Concatenate or &
For date manipulation:
Datevalue
Date
Year
Month
Day
For time manipulation:
Timevalue
Time
Hour
Minute
Second
You have a couple of options. You can either strip out each individual value and place it in the appropriate date/time individual function or rearrange the date and time as a string that is identifiable by the time/date value functions.
A little background on date and time in Excel. Date are stored as an integer. 1 represented 1900/1/1, 2 represents 1900/1/2 and so on. time is stored as a decimal representing fraction of a day. 00:00 is midnight and stores as zero , 12:00 noon is stored as 0.5 to represent half the day. 24:00 is not an official Excel time, though some function will work with it. To test if a date/time is stored as text or as an excel number, you can test the value of the cell with something like ISTEXT(A1), change the formatting to general and see if the display of the cell info changes, and least reliable is to look at the justification of the information in a cell which is left aligned by default for text and right aligned by default for numbers.
Lets assume your strings are stored in A1 and A2
Because Datevalue can be a little finicky and depends a bit on system settings to determine mm vs dd I go with the method of stripping the individual components. In your case because everything has leading zeros the position of month, day, hour, minute and seconds can be hard coded instead of searching the string starting position of each based of key characters like -, :, +, and space.
Grab the year:
=LEFT(A1,4)
Grab the month:
=MID(A1,6,2)
Grab the Day:
=MID(A1,9,2)
Make the date by dropping the results into DATE as follows:
=DATE(LEFT(A1,4),MID(A1,6,2),MID(A1,9,2))
Grab the hour:
=MID(A1,12,2)
Grab the minutes:
=MID(A1,15,2)
Grab the seconds:
=MID(A1,18,2)
Make the time by dropping the results into TIME as follows:
=TIME(MID(A1,12,2),MID(A1,15,2),MID(A1,18,2))
To grab the timezone I will use TIMEVALUE to demonstrate the other method. This will convert the timezone time or decimal value instead of text like the operations above.
Grab the timezone:
=TIMEVALUE(RIGHT(A1,5)*IF(LEFT(RIGHT(A1,6))="-",-1,1)
Then you just need to combine the results together to get the whole thing in an excel date and time format:
=DATE(LEFT(A1,4),MID(A1,6,2),MID(A1,9,2))+TIME(MID(A1,12,2),MID(A1,15,2),MID(A1,18,2))-TIMEVALUE(RIGHT(A1,5))*IF(LEFT(RIGHT(A1,6))="-",-1,1)
Now that the string is converted to an excel date you need to take the difference in the cells noting that the result is days. Then you need to convert it to seconds by multiplying it by 24*60*60 = 86400.
So if you put the results in B1 and B2 then the formula for difference in seconds would be:
=(B2-B1)*24*60*60
In the example below its (F14-F12). Also note that the cells displaying time or date and time have had custom formating applied to them to dispaly 24 hour clock or date and time combined.
How can I convert this string:
Tue Jan 24 14:59:20 BRT 2017.
Into a date that includes day month year and time and timezone, using Excel functions only.
I have several cells with dates following this format. I have to compute the difference between some of these dates in minutes. I believe that the first step is converting the date to a String to a real date information. Then, I will be able to: order the dates and compute the time between consecutive dates.
Use this formula:
=--(SUBSTITUTE(MID(A1,5,LEN(A1)),"BRT",""))
Then format it to the format you want.
It will now work in math equations.
In excel I currently have data in date time format
For example: "11/10/2007 8:40:58 PM"
I am trying to extract the date and if the time is PM, I add a day.
if the time is AM, the date remains the same.
So since the time is 8:40:58 PM I would want 12/10/2007.
Is there a way in excel to do such a thing using formulas?
Please consider using the following formula. This uses the fact that in Excel in its date-time code uses 1 to represent a full day and fractions to represent the time. If timestamp is in A1 then:
=INT(A1)+IF((A1-INT(A1))>=0.5,1,0)
Regards.
This one works for me.
=IF(A1>0.5, A1+1, A1)
In order to have it only return the date right click on column B, select "Format" the take your pick.
How to round time to nearest hour in Excel, for example:
67:45:00 will be 68:00:00
and
53:14:00 will be 53:00:00
regards
You can use MROUND function like this
=MROUND(A1,"1:00")
Assuming the time is in A1 this will round to closest hour.
=ROUND(A1*24,0)/24
Using ROUNDDOWN will force 67:45:00 to 67:00:00
Using ROUNDUP Will force 67:45:00 to 68:00:00
Same formula, change:
24 to 48 for half hours.
24 to 96 for quarters
If you are grouping hourly in a 24 hour span but the date is included in the time stamp, use the same formula but subtract the datevalue after:
=ROUNDDOWN(A1*24;0)/24-INT(A1)
This is useful if you want to see at what time of day something peaks over a period of time.
Transform it to hours (5h 15m = 5.25h) then round it
if you only have it as a string use
=if(round(mid(A1;4;2);0)>29;mid(A1;1;2)+1&":00:00";mid(A1;1;2)&":00:00")
i use round to convert the minutes into a number
I recently had to convert times to the nearest quarter hour. I used the following sequence of formulas, which seemed to work:
=SUM(A1*24*60) - this converts the time to minutes
=MOD(B1,15) - this finds the minutes since the last quarter hour
=IF(C1>7,15-C1,-C1) - minutes needed to round up or down to nearest quarter hour
=SUM(D1/(24*60)) - converts the adjustment needed from minutes back to a days
=SUM(A1+E1) - this is the original time adjusted up or down to the nearest quarter hour
This sounds simple but I have been pulling my hair out trying to figure out a solution. Any help before I go bald would be great.
I need a formula which is able to
calculate the duration in (days, hrs, mins) between two date\time values (eg 05/12/2012 5:30 PM and say 07/12/2012 5:45 PM);
excluding weekends and holidays.
I would like the result of the formula to read as follows "e.g 2 Days 0 Hrs and 15 Mins".
Thanks
Link to sample workbook
You can use NETWORKDAYS and NETWORKDAYS.INTL to achieve this
A bit of manipulation is required as these return net whole days:
Use these functions to calculate the number of non workdays, then subtract from the difference between start and end dates
=E3-D3-(NETWORKDAYS.INTL(D3,E3,"0000000")-NETWORKDAYS(D3,E3,$A$16:$A$24))
This returns the working day difference, where 1.0 = 1 day
NETWORKDAYS.INTL(D3,E3,"0000000") calculates whole days between the two dates (no weekends, no holidays)
NETWORKDAYS(D3,E3,"0000000",$A$16:$A$24) calculates whole working days days between the two dates (Sat/Sun weekends, holidays as per your list in $A$16:$A$24)
Difference in non-working days between the two dates.
E3-D3 is time between start and end date/times (1.0 = 1 day)
Use custom number formatting to display thye result in the format you require
d "Days" h "Hours" mm "Mins"
Note: this format won't work for negative values, you will need an alternative for when end date is before start date.
The following formula works like a treat with no additional formatting or manipulation.
To make it more stable I have turned all the holiday dates for UK 2012/13 into ‘Excel Serial Number’ format and placed them in an array bracket.
Replacing the "D5" in the formula with your cell reference for your course or metric "End Date" and "E5" with your course or Metric for "Completion Date".
=IF(E5<D5,"-"&TEXT(D5-E5,"h:mm"),NETWORKDAYS(D5,E5,({40910,41005,41008,41036,41064,41065,41148,41268,41269,41275,41362,41365,41400,41421,41512,41633,41634}))-1-(MOD(E5,1)<MOD(D5,1))&" days "&TEXT(E5-D5,"h:mm"))