首页 > 解决方案 > 使用给定日期获取那一年中的哪一天

问题描述

例如,我给了 31-dec-2020 它应该返回 365th 我在下面的 bash 中尝试过,但它不起作用。

start_of_year= "2020-01-01"
echo "enter date in yyy-mm-dd format"
read current_date
difference=  `expr $current_date - $start_of_year`
echo "day of the year $difference"

标签: bashdate

解决方案


date -d "$current_date" +%j

格式化程序返回一年中的%j天数。见人日期


推荐阅读