Làm thế nào để bạn trừ thời gian trong python?

ToyMax có 200 nhân viên. Tất cả nhân viên phải quẹt thẻ ra vào hàng ngày bằng đầu đọc thẻ chấm công. Đôi khi thời gian vuốt vào của nhân viên có vẻ không chính xác. Họ dường như đã tắt trong năm (5) phút

Đối với các ví dụ trong bài viết này, thời gian hiện tại cho thời gian vuốt vào được sử dụng


💬 câu hỏi. Làm thế nào chúng ta sẽ viết mã để trừ phút từ một ngày?

Chúng ta có thể hoàn thành nhiệm vụ này bằng một trong các tùy chọn sau

  • Phương pháp 1. Sử dụng
    import arrow
    
    swipe_in  = arrow.utcnow()
    new_swipe_in = swipe_in.shift(minutes=-5)
    print(new_swipe_in)
    1
  • Phương pháp 2. Sử dụng
    import arrow
    
    swipe_in  = arrow.utcnow()
    new_swipe_in = swipe_in.shift(minutes=-5)
    print(new_swipe_in)
    2
  • Phương pháp 3. Sử dụng
    import arrow
    
    swipe_in  = arrow.utcnow()
    new_swipe_in = swipe_in.shift(minutes=-5)
    print(new_swipe_in)
    3
  • Phương pháp 4. Sử dụng
    import arrow
    
    swipe_in  = arrow.utcnow()
    new_swipe_in = swipe_in.shift(minutes=-5)
    print(new_swipe_in)
    4 và
    import arrow
    
    swipe_in  = arrow.utcnow()
    new_swipe_in = swipe_in.shift(minutes=-5)
    print(new_swipe_in)
    1

Phương pháp 1. Sử dụng timedelta()

Ví dụ này sử dụng

import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
1 từ thư viện
import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
7 để trừ năm (5) phút từ một ngày giờ cụ thể. Trong trường hợp này, datetime hiện tại

import datetime
from datetime import timedelta   

swipe_in  = datetime.datetime.today()
new_swipe_in = (swipe_in - timedelta(minutes=5))
print(new_swipe_in)

Dòng đầu tiên trong đoạn mã trên nhập thư viện

import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
7

Dòng sau đây gọi hàm

import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
1 từ thư viện này. Chức năng này cho phép chúng tôi xác định thời lượng để sửa đổi, chẳng hạn như ngày, phút, giờ, v.v. , và thực hiện các hoạt động trên đó

Dòng tiếp theo lấy và lưu datetime hiện tại vào

import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
4. Đối với ví dụ này, nội dung hiển thị bên dưới

import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
5

Dòng sau thực hiện phép trừ bằng cách gọi

import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
1 và truyền cho nó một (1) đối số, thời lượng (
import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
7). Số phút này được trừ vào
import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
4, lưu vào
import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
9 và xuất ra thiết bị đầu cuối

from datetime import datetime
from dateutil.relativedelta import relativedelta

swipe_in  = datetime.now()
new_swipe_in = swipe_in - relativedelta(minutes=5)
print(new_swipe_in)
0

So sánh hai đầu ra, lưu ý rằng

from datetime import datetime
from dateutil.relativedelta import relativedelta

swipe_in  = datetime.now()
new_swipe_in = swipe_in - relativedelta(minutes=5)
print(new_swipe_in)
1 ít hơn năm (5) phút so với
import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
4

Ngày và giờ trong Python - Tổng quan đơn giản

Làm thế nào để bạn trừ thời gian trong python?

Xem video này trên YouTube


Phương pháp 2. Sử dụng shift() từ thư viện mũi tên

Ví dụ này sử dụng hàm

import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
2 từ thư viện
from datetime import datetime
from dateutil.relativedelta import relativedelta

swipe_in  = datetime.now()
new_swipe_in = swipe_in - relativedelta(minutes=5)
print(new_swipe_in)
4 để trừ năm (5) phút từ một
import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
7 đã chỉ định. Trong trường hợp này,
import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
7 hiện tại

Để chạy mã này không bị lỗi, cần cài đặt thư viện

from datetime import datetime
from dateutil.relativedelta import relativedelta

swipe_in  = datetime.now()
new_swipe_in = swipe_in - relativedelta(minutes=5)
print(new_swipe_in)
7. Bấm vào đây để được hướng dẫn

import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)

Dòng đầu tiên này nhập thư viện

from datetime import datetime
from dateutil.relativedelta import relativedelta

swipe_in  = datetime.now()
new_swipe_in = swipe_in - relativedelta(minutes=5)
print(new_swipe_in)
4

Điều này cung cấp cho chúng tôi quyền truy cập vào hàm

from datetime import datetime
from dateutil.relativedelta import relativedelta

swipe_in  = datetime.now()
new_swipe_in = swipe_in - relativedelta(minutes=5)
print(new_swipe_in)
9 để truy xuất ngày giờ hiện tại và
import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
2. Chức năng này cho phép chúng tôi xác định thời lượng để sửa đổi, chẳng hạn như ngày, phút, giờ, v.v. , và thực hiện các hoạt động trên đó

Dòng tiếp theo lấy và lưu datetime hiện tại vào

import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
4. Đối với ví dụ này, nội dung hiển thị bên dưới

import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
5

Dòng sau thực hiện thao tác trừ bằng cách áp dụng

import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
2 và truyền một (1) đối số
import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
64). Kết quả lưu vào
from datetime import datetime
from dateutil.relativedelta import relativedelta

swipe_in  = datetime.now()
new_swipe_in = swipe_in - relativedelta(minutes=5)
print(new_swipe_in)
1 và xuất ra terminal

from datetime import datetime
from dateutil.relativedelta import relativedelta

swipe_in  = datetime.now()
new_swipe_in = swipe_in - relativedelta(minutes=5)
print(new_swipe_in)
0

So sánh hai đầu ra, lưu ý rằng

from datetime import datetime
from dateutil.relativedelta import relativedelta

swipe_in  = datetime.now()
new_swipe_in = swipe_in - relativedelta(minutes=5)
print(new_swipe_in)
1 ít hơn năm (5) phút so với
import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
4


Phương pháp 3. Sử dụng tương đốidelta()

Ví dụ này nhập thư viện

import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
7 để truy xuất ngày hiện tại và thư viện
import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
70 để trừ năm (5) phút từ một ngày giờ đã chỉ định. Trong trường hợp này, datetime hiện tại

Để chạy mã này không bị lỗi, cần cài đặt thư viện

import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
70. Bấm vào đây để được hướng dẫn

from datetime import datetime
from dateutil.relativedelta import relativedelta

swipe_in  = datetime.now()
new_swipe_in = swipe_in - relativedelta(minutes=5)
print(new_swipe_in)

Dòng đầu tiên nhập thư viện

import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
7 được nhập cho phép chúng tôi truy cập vào hàm
import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
73. Hàm này lấy datetime hiện tại

Dòng tiếp theo lấy và lưu datetime hiện tại vào

import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
4. Đối với ví dụ này, nội dung hiển thị bên dưới

import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
5

Dòng sau thực hiện phép trừ bằng cách gọi

import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
3 và truyền cho nó một (1) đối số, thời lượng (
import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
7). Điều này được trừ từ
import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
4, được lưu vào
import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
9 và xuất ra thiết bị đầu cuối.

from datetime import datetime
from dateutil.relativedelta import relativedelta

swipe_in  = datetime.now()
new_swipe_in = swipe_in - relativedelta(minutes=5)
print(new_swipe_in)
0

So sánh hai đầu ra, lưu ý rằng

from datetime import datetime
from dateutil.relativedelta import relativedelta

swipe_in  = datetime.now()
new_swipe_in = swipe_in - relativedelta(minutes=5)
print(new_swipe_in)
1 ít hơn năm (5) phút so với
import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
4


Phương pháp 4. gấu trúc và timedelta()

Ví dụ này nhập thư viện gấu trúc và hàm

import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
1 để trừ năm (5) phút từ một cột DataFrame được chỉ định

Để chạy mã này không bị lỗi, cần cài đặt thư viện

import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
14. Bấm vào đây để được hướng dẫn

Để làm theo, hãy lưu dữ liệu bên dưới dưới dạng

import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
15 và đặt dữ liệu này vào thư mục làm việc hiện tại

IDSwipeInSwipeOut1982022-10-03 07. 05. 57. 1397882022-10-03 17. 00. 57. 1208641992022-10-03 07. 05. 57. 2208642022-10-03 17. 00. 57. 2208642002022-10-03 07. 05. 56. 4808642022-10-03 17. 00. 57. 3208642012022-10-03 07. 05. 56. 4770642022-10-03 17. 00. 57. 4208642022022-10-03 07. 05. 56. 4108642022-10-03 17. 00. 57. 620864____16

Dòng đầu tiên nhập thư viện

import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
14. Điều này cho phép chúng tôi làm việc với các tệp CSV và DataFrames, chỉ để đặt tên cho một số

Dòng sau gọi hàm

import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
1 từ thư viện
import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
7. Chức năng này cho phép chúng tôi xác định thời lượng để sửa đổi, chẳng hạn như ngày, phút, giờ, v.v. , và thực hiện các hoạt động trên đó

Tiếp theo, chúng tôi đọc tệp

import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
15 được tạo ở trên và lưu tệp này vào DataFrame
import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
20

Dòng sau đây thực hiện tất cả công việc. Ở đây, chúng tôi cộng trừ năm (5) phút cho mỗi Cột DataFrame, thời gian

import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
21. Những kết quả này sau đó được lưu vào tệp CSV mới, tệp
import arrow

swipe_in  = arrow.utcnow()
new_swipe_in = swipe_in.shift(minutes=-5)
print(new_swipe_in)
22. Nội dung của tập tin này được hiển thị bên dưới

IDSwipeInSwipeOut1982022-10-03 07. 00. 57. 1397882022-10-03 17. 00. 57. 1208641992022-10-03 07. 00. 57. 2208642022-10-03 17. 00. 57. 2208642002022-10-03 07. 00. 56. 4808642022-10-03 17. 00. 57. 3208642012022-10-03 07. 00. 56. 4770642022-10-03 17. 00. 57. 4208642022022-10-03 07. 00. 56. 4108642022-10-03 17. 00. 57. 620864

10 phút để gấu trúc trong 5 phút (Được rồi 8)

Làm thế nào để bạn trừ thời gian trong python?

Xem video này trên YouTube


Tóm lược

Bốn (4) phương pháp trừ ngày này sẽ cung cấp cho bạn đủ thông tin để chọn phương pháp tốt nhất cho các yêu cầu viết mã của bạn

Làm cách nào để trừ thời gian khỏi DateTime Python?

Bạn có thể trừ một ngày từ ngày trong python bằng cách sử dụng đối tượng timedelta. Bạn cần tạo một đối tượng timedelta với lượng thời gian bạn muốn trừ. Sau đó trừ đi ngày .

Có chức năng trừ trong Python không?

trừ () trong Python. numpy. hàm Subtract() được sử dụng khi chúng ta muốn tính hiệu của hai mảng . Nó trả về sự khác biệt của mảng1 và mảng2, phần tử khôn ngoan.