Score:0

Bash: Offset a date by n seconds

cn flag
SEU

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
terdon avatar
cn flag
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.
Score:0
my flag
# 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')
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.