Python | fonction datetime.timedelta()

Python delta de temps() la fonction est présente sous bibliothèque date/heure qui est généralement utilisé pour calculer les différences de dates et peut également être utilisé pour les manipulations de dates en Python. C’est l’un des moyens les plus simples d’effectuer des manipulations de dates.

Syntaxe : datetime.timedelta(jours=0, secondes=0, microsecondes=0, millisecondes=0, minutes=0, heures=0, semaines=0)
Retour : Date

Code n°1 :

Python3




# Timedelta function demonstration> from> datetime> import> datetime, timedelta> # Using current time> ini_time_for_now> => datetime.now()> # printing initial_date> print> ('initial_date',> str> (ini_time_for_now))> # Calculating future dates> # for two years> future_date_after_2yrs> => ini_time_for_now> +> > > timedelta(days> => 730> )> future_date_after_2days> => ini_time_for_now> +> > > timedelta(days> => 2> )> # printing calculated future_dates> print> (> 'future_date_after_2yrs:'> ,> str> (future_date_after_2yrs))> print> (> 'future_date_after_2days:'> ,> str> (future_date_after_2days))>

Sortir:

initial_date 2019-02-27 12:41:45.018389 future_date_after_2yrs: 2021-02-26 12:41:45.018389 future_date_after_2days: 2019-03-01 12:41:45.018389 

Code n°2 :

Python3




# Timedelta function demonstration> from> datetime> import> datetime, timedelta> # Using current time> ini_time_for_now> => datetime.now()> # printing initial_date> print> (> 'initial_date:'> ,> str> (ini_time_for_now))> # Calculating past dates> # for two years> past_date_before_2yrs> => ini_time_for_now> -> > > timedelta(days> => 730> )> # for two hours> past_date_before_2hours> => ini_time_for_now> -> > > timedelta(hours> => 2> )> # printing calculated past_dates> print> (> 'past_date_before_2yrs:'> ,> str> (past_date_before_2yrs))> print> (> 'past_date_before_2hours:'> ,> str> (past_date_before_2hours))>

Sortir:

initial_date 2019-02-27 12:41:46.104662 past_date_before_2yrs: 2017-02-27 12:41:46.104662 past_date_after_2days: 2019-02-27 10:41:46.104662 

Code n°3 :

Python3




# Timedelta function demonstration> from> datetime> import> datetime, timedelta> # Using current time> ini_time_for_now> => datetime.now()> # printing initial_date> print> ('initial_date',> str> (ini_time_for_now))> # Some another datetime> new_final_time> => ini_time_for_now> +> > > timedelta(days> => 2> )> # printing new final_date> print> ('new_final_time',> str> (new_final_time))> # printing calculated past_dates> print> (> 'Time difference:'> ,> str> (new_final_time> -> > > ini_time_for_now))>

Sortir:

initial_date 2019-02-27 12:41:47.386595 new_final_time 2019-03-01 12:41:47.386595 Time difference: 2 days, 0:00:00 

Soustraire un objet timedelta d'un objet date ou datetime :

Approche:

Créez un objet timedelta avec le décalage horaire souhaité.
Soustrayez l’objet timedelta d’un objet date ou datetime à l’aide de l’opérateur –.

Python3




import> datetime> today> => datetime.date.today()> three_days_ago> => today> -> datetime.timedelta(days> => 3> )> print> (> 'Today:'> , today)> print> (> 'Three days ago:'> , three_days_ago)>

Sortir

Today: 2023-03-24 Three days ago: 2023-03-21 

Complexité temporelle :O(1)
Espace auxiliaire : O(1)