unix 中的时间戳变量

问题描述 投票:0回答:2

我在 bash 脚本变量 exp_dt 中得到以下数据:

exho $exp_dt
Jun  5 08:25:42 2023 GMT

需要帮助将其转换为类似“05-Jun-2023”的格式和当前日期剩余的天数,即 64天。

预期产出

echo $new_var
05-Jun-2023|64 days
bash shell unix unix-timestamp
2个回答
1
投票
ts='Jun  5 08:25:42 2023 GMT'

echo "${ts}"

d=$(date -d "${ts}" +%d-%b-%Y)
sec=$(date -d "${ts}" +%s)
now=$(date +%s)
diff=$(expr $sec - $now)

days=$(expr $diff / 86400 )
new_var="$d|$days days"

echo $new_var

跑步:

penguin:~$ sh d.sh 
Jun  5 08:25:42 2023 GMT
05-Jun-2023|64 days
penguin:~$

1
投票

试试这个:

# your date
exp_dt="Jun  5 08:25:42 2023 GMT"

# actual day of year
b=$(date "+%-j")

# day of year of your date
c=$(date -d "$exp_dt" "+%-j")

# diffence of days
new_var=$((c-b))

# your solution
echo $(date -d "$exp_dt" "+%d-%m-%Y") "| $new_var days"
© www.soinside.com 2019 - 2024. All rights reserved.