简体   繁体   中英

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?

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

不过,很可能在30分钟前的同一天:)

您可以附加格式字符串:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM