Lucio Paoletti
Lucio Paoletti

Reputation: 371

Date format in bash

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?

Upvotes: 1

Views: 952

Answers (3)

Clarkey
Clarkey

Reputation: 1581

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.

Upvotes: -1

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143051

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

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

Upvotes: 5

j0nes
j0nes

Reputation: 8099

You can append a format string:

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

Upvotes: 4

Related Questions