shell 实现获取当前时间,并进行格式转换的方法:
1)原格式输出

[root@iZbp18re0hh4zxb0fzwttcZ ~]# time1=$(date)
[root@iZbp18re0hh4zxb0fzwttcZ ~]# echo $time1

2)时间串输出

[root@iZbp18re0hh4zxb0fzwttcZ ~]# time2=$(date "+%Y%m%d%H%M%S")
[root@iZbp18re0hh4zxb0fzwttcZ ~]# echo $time2
20211214152806

[root@iZbp18re0hh4zxb0fzwttcZ ~]# time3=$(date "+%Y-%m-%d %H:%M:%S")
[root@iZbp18re0hh4zxb0fzwttcZ ~]# echo $time3
2021-12-14 15:30:36

[root@iZbp18re0hh4zxb0fzwttcZ shell]# time4=$(date "+%Y.%m.%d")
[root@iZbp18re0hh4zxb0fzwttcZ shell]# echo $time4
2021.12.14

[root@iZbp18re0hh4zxb0fzwttcZ system]# time5=$(date "+%D")
[root@iZbp18re0hh4zxb0fzwttcZ system]# echo $time5
12/14/21

解释

Y显示4位年份,如:2018;y显示2位年份,如:18。

m表示月份;M表示分钟。

d代表天;D则表示当前日期。

H 表示小时,而h显示月份。

s显示当前秒钟,单位为毫秒;S显示当前秒钟,单位为秒。
————————————————

posted on 2022-05-15 22:25  胖娃  阅读(2059)  评论(0编辑  收藏  举报