SHELL script number less than 10 is removed date leading zero

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/m0_37886429/article/details/102550844

method one:

#!/bin/bash

tmonth=`date +%m`
tyear=`date +%y`
tday=`date +%d`
day=`expr $tday + 0`       #去除日期前面的零
month=`expr $tmonth + 0`   #去除月份前面的零
year=`expr $tyear + '2000'`
now_date=`printf "%04d/%d/%d" $year $month $day`
echo $now_date

结果:
2019/10/1

Method Two:

echo `date +%Y-%m-%d` | awk -F"-" '{printf("date is %d/%d/%d\n",$1,$2,$3)}'
date is 2019/10/1

Added: get a different date and time of the command

date -d next-day +%Y%m%d   #明天日期
date -d tomorrow +%Y%m%d   #明天日期
date -d last-day +%Y%m%d   #昨天日期
date -d yesterday +%Y%m%d  #昨天日期
date -d last-month +%Y%m   #上个月日期
date -d next-month +%Y%m   #下个月日期
date -d next-year +%Y      #明年日期

 
tmonth=`date -d last-day +%m`
tyear=`date -d last-day +%y`
tday=`date -d last-day +%d`
day=`expr $tday + 0`
month=`expr $tmonth + 0`
year=`expr $tyear + '2000'`
now_date=`printf "%04d/%d/%d" $year $month $day`
echo $now_date

Guess you like

Origin blog.csdn.net/m0_37886429/article/details/102550844