Administrator
发布于 2023-07-21 / 52 阅读
0
0

Linux Date Associated Command

Date命令

refer to : https://man7.org/linux/man-pages/man1/date.1.html

获取当前时间

[root@VM-0-3-opencloudos test]# date
Fri Jul 21 11:03:51 CST 2023

DESCRIPTION

Display date and time in the given FORMAT. With -s, or with
[MMDDhhmm[[CC]YY][.ss]], set the date and time.

   Mandatory arguments to long options are mandatory for short
   options too.

   -d, --date=STRING
          display time described by STRING, not 'now'

实例

查看当前时间,显示成年月日时分秒的形式

[root@VM-0-3-opencloudos test]# date +"%Y-%m-%d %H:%M:%S"
2023-07-21 11:06:14

查看今天的日期

[root@VM-0-3-opencloudos test]# date +"%Y-%m-%d"
2023-07-21

查看明天的日期

[root@VM-0-3-opencloudos test]# date -d "+1 day" +"%Y-%m-%d"
2023-07-22

查看当前的时间戳

#!/bin/bash

current=`date "+%Y-%m-%d %H:%M:%S"`
timeStamp=`date -d "$current" +%s`
currentTimeStamp=$((timeStamp*1000+10#`date "+%N"`/1000000)) #将current转换为时间戳,精确到毫秒
echo $currentTimeStamp

查看明天凌晨的时间戳

#!/bin/bash

tomorrow=`date -d "+1 day" +"%Y-%m-%d"`
timeStamp=`date -d "$tomorrow 00:00:00" +%s`
currentTimeStamp=$(($timeStamp*1000)) #将current转换为时间戳,精确到毫秒
echo $currentTimeStamp

查看昨天凌晨的时间戳

#!/bin/bash

yesterday=`date -d "-1 day" +"%Y-%m-%d"`
yesSecond=`date -d "$yesterday 00:00:00" +%s`
yesMs=$(($yesSecond*1000)) #将current转换为时间戳,精确到毫秒
echo $yesMs


评论