跳到主要內容

如何在Excel中午夜之後的時間之間計算小時?

doc計算過去一夜的時間1

假設您有一個時間表來記錄您的工作時間,則A列中的時間是今天的開始時間,B列中的時間是第二天的結束時間。 通常,如果您直接減去“ = B2-A2”來計算兩次之間的時間差,則不會顯示正確的結果,如左圖所示。 如何在Excel中正確計算午夜後兩次之間的小時數?

使用公式計算午夜後兩次之間的小時數


箭頭藍色右氣泡 使用公式計算午夜後兩次之間的小時數

要在午夜兩次之間獲得正確的計算結果,您可以應用以下公式:

1。 輸入以下公式: =(B2-A2+(B2<A2))*24 (A2 是較早的時間, B2 是以後的時間,您可以根據需要將它們更改為時間數據旁邊的空白單元格,請參見屏幕截圖:

doc計算過去一夜的時間2

2. 然後將填充柄拖到要填充此公式的單元格上,並立即計算午夜後兩次之間的時間差,見截圖:

doc計算過去一夜的時間3

最佳辦公生產力工具

🤖 Kutools 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行   |  生成代碼  |  建立自訂公式  |  分析數據並產生圖表  |  呼叫 Kutools 函數...
熱門特色: 尋找、突出顯示或識別重複項   |  刪除空白行   |  合併列或儲存格而不遺失數據   |   沒有公式的回合 ...
超級查詢: 多條件VLookup    多值VLookup  |   跨多個工作表的 VLookup   |   模糊查詢 ....
高級下拉列表: 快速建立下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
欄目經理: 新增特定數量的列  |  移動列  |  切換隱藏列的可見性狀態  |  比較範圍和列 ...
特色功能: 網格焦點   |  設計圖   |   大方程式酒吧    工作簿和工作表管理器   |  資源庫 (自動文字)   |  日期選擇器   |  合併工作表   |  加密/解密單元格    按清單發送電子郵件   |  超級濾鏡   |   特殊過濾器 (過濾粗體/斜體/刪除線...)...
前 15 個工具集12 文本 工具 (添加文本, 刪除字符,...)   |   50+ 圖表 類型 (甘特圖,...)   |   40+ 實用 公式 (根據生日計算年齡,...)   |   19 插入 工具 (插入二維碼, 從路徑插入圖片,...)   |   12 轉化 工具 (數字到單詞, 貨幣兌換,...)   |   7 合併與拆分 工具 (高級合併行, 分裂細胞,...)   |   ... 和更多

使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。  點擊此處獲取您最需要的功能...

產品描述


Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!
Comments (23)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hi All,

So I have a list of employees, all their working dates with start and end time & date.

I need to figure out how many hours were spent after midnight- night hours. The issue is that there are all sort of starting and finish times- some of the shifts were day shift the others started at 9pm till 2am, others started 10pm till next morning 5 am, etc.

Is there a way of calculating this using a generic formula for all lines?

I have tried so many variations but excel does not want to collaborate!
This comment was minimized by the moderator on the site
Hello, Timea,
In fact, the formula in this article can solve your problem, see below screenshot:
https://www.extendoffice.com/images/stories/comments/comment-skyyang/2023-comment/doc-calculate-hours.png
Please have a try, thank you!
This comment was minimized by the moderator on the site
Thank you for your reply.

I only want to know the hours worked after midnight, so only the night hours.
Not total hours worked.
Hope this makes sense.
This comment was minimized by the moderator on the site
Hello, Timea
If you just need to get the hours worked after midnight, the below formula may help you:
=IF(DAYS(B2,A2)>0,(B2-A2+(B2<A2))*24,"")
Please try, hope it can help you!
This comment was minimized by the moderator on the site
Very clever, using the comparison (b2<a2) to return a boolean result so if it's after midnight it adds 1.  It took me a minute to understand what you did it was so elegant. 
This comment was minimized by the moderator on the site
Hello,
You are welcome. Glad it helps. Any questions, please feel free to contact us. Have a great day.
Sincerely,
Mandy
This comment was minimized by the moderator on the site
This doesn't work with all data. The solution I tried after trying the above was =IF(B22>C22,(C22-0)+(24-B22),C22-B22)
This comment was minimized by the moderator on the site
=IF(B22>C22,(C22-0)+(24-B22),C22-B22) is perfect but instead of ( , ) use ( ; )
thankyouverymuch
This comment was minimized by the moderator on the site
Doesn't work for me... Formula incorrect.
This comment was minimized by the moderator on the site
Great solution, very useful! Thanks :)
This comment was minimized by the moderator on the site
Hi, i need help please. I have data that auto loads daily at 5pm. The process is broken into 2 parts (ETL1 and ETL2). ETL1 starts at 5pm till 11pm and ETL2 starts at 11 till 4 am. at 7 am i run a script to check if everything ran. each row has a start and end date time. i would like to flag all data after 5pm yesterday as todays data. Currently when i filter on today, i only see the rows where the date is after 00:00
This comment was minimized by the moderator on the site
Can someone explain when the function of addition of the TRUE/FALSE values does so I can understand the formula please?
This comment was minimized by the moderator on the site
True = 1, False = 0
This comment was minimized by the moderator on the site
I think a simple MOD(B2-A2,1) should be enough?
This comment was minimized by the moderator on the site
This is undoubtedly the best and shortest solution.
This comment was minimized by the moderator on the site
Austin, the formula stated in the article above works well for me
This comment was minimized by the moderator on the site
The formula under bullet point #1 is wrong. =(B2-A2+(B2<A2))*24, but is should read as =B2-A2+(B2<A2)*24.

The parentheses are in the wrong spot.
This comment was minimized by the moderator on the site
THANK YOU! This is definitely the correct formula.
This comment was minimized by the moderator on the site
Yes Austin! Your formula works, the other formula renders nonsense (in my case). Thanks!!
This comment was minimized by the moderator on the site
No, Austin's correction works for me. The original with the parentheses where they are shown does not work. Maybe a diifferent version of Excel makes a difference? I am on MS Home and Office 2016
This comment was minimized by the moderator on the site
The original formula works better than your suggestion Austin.
I also used =(A2-B2+(A2<B2))*1440
This worked best to convert time into minutes.
This comment was minimized by the moderator on the site
Depois de colocar a formatação ao tentar somar a coluna total de horas o valor dá-me errado.

O campo é formatado [h]mm
This comment was minimized by the moderator on the site
=(A2-B2+(A2)) sem utilizar a multiplicação por 24
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations