Diviértete con la sintaxis de fecha en el entorno Linux

Diviértete con la sintaxis de fecha en el entorno Linux

Edición ⑦ | 20 libros técnicos, ¡envío gratis!

Cuando acaba de comenzar a aprender Linux, puede ejecutar un comando de fecha para verificar la hora, pero es posible que el formato de fecha y hora y el contenido de la fecha no sea lo que queremos, entonces necesitamos generar más salida para lograr el formato que queremos. Hoy, le mostraré la gramática básica de la fecha en el entorno Linux, también puede operarlo en la computadora, el efecto será mejor. La sintaxis de cada consulta en el cuadro de terminales a continuación es ligeramente diferente, puede deslizar hacia la izquierda y hacia la derecha para verla ~

Campos de fecha y hora de uso común

Diviértete con la sintaxis de fecha en el entorno Linux

Diviértete con la sintaxis de fecha en el entorno Linux
Al usar el comando de fecha, puede obtener la fecha y hora actual del sistema sin ningún parámetro. La hora está generalmente en formato estándar CST.


[root@localhost ~]# date
2019年 04月 08日 星期一 09:43:37CST

Además de lo anterior sin ningún parámetro, también puede obtener la hora actual por fecha +% r;


[root@localhost ~]# date +%r
下午 01时22分06秒

Para obtener un formato específico, el comando es: fecha + 'formato', tenga en cuenta que: el caso del dominio del tiempo.


[root@localhost ~]# date +‘%Y-%m-%d’         #以中文的引号进行输出
‘2019-04-08’
[root@localhost ~]# date +%Y-%m-%d           #不带引号的输出格式
2019-04-08
[root@localhost ~]# date +'%Y-%m-%d'          #以英文的引号进行输出
2019-04-08
[root@localhost ~]# date +'%Y/%m/%d %H:%M:%S'    #特定格式,进行显示年月日,时分秒
2019/04/08 09:47:44

Obtenga la fecha (año, mes, día, hora, minuto y segundo) de ayer, anteayer y hace unos días, y se puede agregar el parámetro -d.


[root@localhost ~]# date -d 'yesterday'
2019年 04月 07日 星期日 09:48:41 CST
[root@localhost ~]# date -d last-day +%Y%m%d
20190407
[root@localhost ~]# date -d 'yesterday' +'%Y/%m/%d %H:%M:%S'
2019/04/07 10:02:26
[root@localhost ~]# date -d last-day +'%Y%m%d %H:%M:%S'
20190407 10:43:04
[root@localhost ~]# date -d 'today -1 day' +'%Y-%m-%d'
2019-04-07
[root@localhost ~]# date -d 'yesterday -1 day' +'%Y-%m-%d'
2019-04-06
[root@localhost ~]# date -d 'yesterday -1 day' +'%Y-%m-%d %H:%M:%S' #当前日期前两天的时间点
2019-04-06 11:09:35
[root@localhost ~]# date -d "6 days ago" +'%Y-%m-%d %H:%M:%S'    #当前日期前6天的时间点
2019-04-02 11:13:23

Obtenga la sintaxis de búsqueda para mañana, pasado mañana y los días posteriores a la fecha actual;


[root@localhost ~]# date -d 'tomorrow'
2019年 04月 09日 星期二 10:51:21 CST
[root@localhost ~]# date -d tomorrow +%Y%m%d
20190409
[root@localhost ~]# date -d 'tomorrow' +'%Y%m%d %H:%M:%S'
20190409 10:53:56
[root@localhost ~]# date -d tomorrow +'%Y%m%d %H:%M:%S'
20190409 10:54:57
[root@localhost ~]# date -d tomorrow +'%Y-%m-%d %H:%M:%S'
2019-04-09 10:56:09
[root@localhost ~]# date -d tomorrow +'%Y-%m-%d'
2019-04-09
[root@localhost ~]# date -d next-day +%Y%m%d
20190409
[root@localhost ~]# date -d next-day +'%Y-%m-%d'
2019-04-09
[root@localhost ~]# date -d 'next-day' +'%Y%m%d %H:%M:%S'
20190409 10:54:06
[root@localhost ~]# date -d next-day +'%Y%m%d %H:%M:%S'
20190409 10:55:12
[root@localhost ~]# date -d next-day +'%Y-%m-%d %H:%M:%S'
2019-04-09 10:55:59
[root@localhost ~]# date -d 'tomorrow -l' +'%Y-%m-%d' #当前日期后天的年月日
2019-04-10
[root@localhost ~]# date -d 'tomorrow -l day' +'%Y-%m-%d' #当前日期后天的年月日
2019-04-11
[root@localhost ~]# date -d "-6 days ago" +'%Y-%m-%d %H:%M:%S' #当前日期后6天的时间点
2019-04-14 11:14:51

Obtiene la sintaxis de la hora anterior a la hora actual;


[root@localhost ~]# date -d 'today -1 hour' +'%Y%m%d%H'
2019040810
[root@localhost ~]# date +'%Y-%m-%d %H:%M:%S' -d '-1 hours'
2019-04-08 10:22:01

Obtiene la sintaxis de fecha del día anterior y posterior a la fecha especificada;


[root@localhost ~]# date -d '20190101 -1 day' +'%Y%m%d'
20181231
[root@localhost ~]# date -d '20190101 yesterday' +'%Y%m%d'
20181231
[root@localhost ~]# date -d '20190101 yesterday -5 day' +'%Y%m%d'
20181226
[root@localhost ~]# date -d '20190101 yesterday' +'%Y%m%d %H:%M:%S'
20181231 00:00:00
[root@localhost ~]# date -d '20190101 yesterday -5 day' +'%Y%m%d %H:%M:%S'
20181226 00:00:00
[root@localhost ~]# date -d '20190101 1 day' +'%Y%m%d'
20190102
[root@localhost ~]# date -d '20190101 -l day' +'%Y%m%d'
20190102
[root@localhost ~]# date -d '20190101 tomorrow' +'%Y%m%d'
20190102
[root@localhost ~]# date -d '20190101 tomorrow 6 day' +'%Y%m%d'
20190108
[root@localhost ~]# date -d '20190101 tomorrow' +'%Y%m%d %H:%M:%S'
20190102 00:00:00
[root@localhost ~]# date -d '20190101 tomorrow 6 day' +'%Y%m%d %H:%M:%S'
20190108 00:00:00

Obtenga la gramática de fecha y hora hace una semana;


[root@localhost ~]# date -d "1 week ago" +%Y%m%d
20190401
[root@localhost ~]# date -d "1 week ago" +'%Y%m%d %H:%M:%S'
20190401 12:56:04

Obtenga el año y el mes del mes anterior y el mes siguiente;


[root@localhost ~]# date -d last-month +%Y%m
201903
[root@localhost ~]# date -d next-month +%Y%m
201905

Obtenga el año del año anterior y el año del año siguiente;


[root@localhost ~]# date -d last-year +%Y
2018
[root@localhost ~]# date -d next-year +%Y
2020

Obtenga la gramática de fecha y hora hace un año;


[root@localhost ~]# date -d "1 year ago" +%Y%m%d
20180408
[root@localhost ~]# date -d "1 year ago" +'%Y%m%d %H:%M:%S'
20180408 12:58:10

Obtener la fecha actual es el día del año;


[root@localhost ~]# date "+%j"
098

Obtenga el mes / día / año actual;


[root@localhost ~]# date +%D
04/08/19

Para obtener el calendario mensual de todo el año, ejecute el comando: cal -y to view;

Diviértete con la sintaxis de fecha en el entorno Linux

Obtenga el mes actual y los dos meses anteriores y siguientes del calendario, ejecute el comando: cal -3 para ver; si no se agregan parámetros, se mostrará el calendario del mes actual;

Diviértete con la sintaxis de fecha en el entorno Linux

Convertir fecha en marca de tiempo

[root@localhost ~]# date -d "Mar 6 23:59:59CST 2019" +%s
1551887999

Convertir la marca de tiempo a la fecha


[root@localhost ~]# date -d @1551887999
2019年 03月 06日 星期三 23:59:59CST

Convierta la marca de tiempo en una fecha y envíela en un formato específico.


[root@localhost ~]# date -d @1551887999 +'%Y%m%d %H:%M:%S'
20190306 23:59:59

Supongo que te gusta

Origin blog.51cto.com/15067236/2607538
Recomendado
Clasificación