1

Simple bash question... I suppose, I'm new.

I have substract date from system time

date_from=`date -d "30 minutes ago"` 

after, I want format the result in $date_from in 'yyyy-mm-dd'

how can I do that?

3 Answers 3

5
date -d "30 minutes ago" +%Y-%m-%d 

It is very likely, though, that 30 minutes ago it was the same day :)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I know Michael, this is for script running every five minutes on 24h ^_^
4

You can append a format string:

date -d "30 minutes ago" +"%Y-%m-%d" 

Comments

-1

As you can't guarantee that 30 minutes ago would be the same day, your best solution would be convert the current date/time into seconds from 1970, subtract 30*60 seconds, and then convert this back into a date.

I could do this in a script, not sure how to do it in one line.

Something like:

CURRENT=date +%S CURRENTMINUS30=expr $CURRENT - (30*60) OLD = date -d@`CURRENTMINUS30` 

That's untested though. I'll have a go at getting the script to work and post it's contents, and perhaps someone else can do it in one line.

2 Comments

I don't see the need to convert to a UNIX timestamp first. date can handle the change in day just fine if necessary.
Ah OK. Totally misunderstood the other answers. Didn't realise "30 minutes ago" was a valid input for date! I just thought they were unfinished answers. Every day's a school day.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.