0

I am trying to offset a time in the following format. I am unable to get this working. Any suggestions?

This doesn't work

DATE0="26Sep21 06:10:14"
DATE1=$(date -d "$DATE0 + 1 seconds"  +'%d%b%y %H:%M:%S'); 
echo $DATE1

This doesn't work either

n=10
DATE0="26Sep21 06:10:14"
DATE1=$(date -d "$DATE0 + $n seconds"  +'%d%b%y %H:%M:%S'); 
echo $DATE1
1
  • How does it not work? What output are you getting and what output are you expecting? Both of your examples work fine on my system.
    – terdon
    Sep 26, 2021 at 13:48

1 Answer 1

0
# Init
n=10
DATE0="26Sep21 06:10:14"

# Seconds since 01/01/1970
SECONDS=$(date +%s --date "${DATE0}")

# Add seconds
SECONDS=$(( SECONDS + n ))

# Reformat seconds
DATE1=$(date --date "@${SECONDS}" +'%d%b%y %H:%M:%S')

One line:

# Init
n=10
DATE0="26Sep21 06:10:14"

# Compute
DATE1=$(date --date "@$(( $(date +%s --date "${DATE0}") + n ))" +'%d%b%y %H:%M:%S')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.