Skip to content

Datetime

basic

$(date "+%F %H:%M:%S"); #datatime

# 2 days ago
$(date -d "2 days ago" +"%Y-%m-%d"); #current date -2 days, not supported by all platforms
echo $(date -d@"$(($(date +%s) - $((2*86400))))" +"%Y-%m-%d") #more basic

# 2 days later
$(date --date='+2 days' +%F); #current date +2 days in format yyyy-mm-dd
$(date -d@"$(( $(date +%s) + 2 * 86400 ))" +"%Y-%m-%d"); #stripped more basic version

day of month

if [[ $(date +%d) == 1 ]]; then r=360; else r=30; fi
if [[ $(date +%d) == 1 && $(( $(date +%m)%2 )) == 1 ]]; then r=180; fi #month is odd

example of scripts for /bin/sh

How to change scripts to work in sh

  1. numdays=$(($NDAYS-1)); This line tries to subtract 1 from the value of NDAYS, but it seems that the shell is interpreting $NDAYS as a string rather than a number. To fix this, you can use double parentheses for arithmetic expansion:

  2. Replace: numdays=$(($NDAYS-1));

  3. With: numdays=$((NDAYS-1));

  4. if [[ $(date +%d) == 10 ]]; then numdays=$((30*6-1)); fi; This is to check if the current day is the 10th day of the month. However, the date format +%d returns the day of the month with leading zeros (e.g., 07 instead of 7). To fix this, you can use the -eq operator for integer comparison and remove the leading zeros:

  5. Replace: if [[ $(date +%d) == 10 ]]; then numdays=$((30*6-1)); fi;

  6. With: if [ $(date +%e) -eq 10 ]; then numdays=$((30*6-1)); fi;

  7. if [[ $(date +%d) == 10 && $(( $(date +%m)%2 )) == 1 ]]; then numdays=$((30*13-1)); fi; Here, we check if it's the 10th day of the month and if the current month is an odd number. However, you should ensure that each opening [ has a corresponding closing ]:

  8. Replace: if [[ $(date +%d) == 10 && $(( $(date +%m)%2 )) == 1 ]]; then numdays=$((30*13-1)); fi;

  9. With: if [ $(date +%e) -eq 10 ] && [ $(( $(date +%-m) % 2 )) -eq 1 ]; then numdays=$((30*13-1)); fi;