Table des matières

Linux

Manipulation sur les dates


Commande 'date'

 $ date [+[format_options]]

 $ date
 Wed Jul 20 16:49:15 CEST 2022
 
 $ date +%Y-%m-%d
 2022-07-20

Format Description
+%a Gives name of the weekday [Mon, Sun, Fri]
+%A Gives name of the weekday [Monday, Sunday, Friday]
+%b Gives name of the month [Jan, Feb, Mar]
+%B Gives name of the month [January, February, March]
+%d Displays day of the month [05]
+%D Displays current date MM/DD/YY format [11-01-21]
+%F Shows date in YYYY-MM-DD format [2021-11-01]
+%H Shows hour in 24-hour format [22]
+%I Shows hour in 12-hour format [11]
+%j Displays the day of the year [001 – 366]
+%m Displays the number of the month [01-12]
+%M Displays minutes [00-59]
+%S Displays seconds [00-59]
+%N Displays in Nanoseconds
+%T Displays time as HH:MM:SS [in 24-hour format]
+%u Day of the week [1-7] 1 is Monday, 6 is Saturday
+%U Shows week number of the year [00-53]
+%Y Displays year YYYY [2021]
+%Z Displays Time zone
Format (avec indicateur)Description
-d “Yesterday” Print yesterday's date and time.
-d “tomorrow” Print Tomorrow date and time.
-d “tomorrow -10 days” Find what is the date and time before 10 days from now.
-d “last month” “%B” Find last month.
-d “next month” “%B” Find next month.
-d “last year” “+%Y” Find last year.
-d “next year” “+%Y” Find next year.
-d “Today +2 days” “+%A” Forecast the weekday


Afficher la date depuis un timestamp

 $ date -d @1381764199
 Mon Oct 14 17:23:19 CEST 2013

Nombre de jours entre 2 dates données

 $ echo $(( ( $(date -d "2022-07-20" "+%s") - $(date -d "2022-01-01" "+%s") ) / 86400))
 199
 # ou
 $ echo $(($((`date +%s --date 01/10/2016`-`date +%s --date 01/10/2015`))/86400)) jours
 365 jours

Identifier une année bissextile

 $ for y in {2000..2010}; do date -d $y-02-29 &>/dev/null && echo "$y est bissextile"; done
 2000 est bissextile
 2004 est bissextile
 2008 est bissextile

Modifier la date et l'heure du système

 $ date --set="Thu Nov 12 13:06:59 IST 2020"
 Thu Nov 12 13:06:59 IST 2020
 $ hwclock --systohc      # synchronise le changement avec l'horloge matérielle