天天看點

linux date 前一天等多種用法

BASH SHELL中可以定義變量顯示目前日期

顯示目前日期是

DATE=`date +%Y%m%d`  +号後面是定義格式為年月日

顯示前一天為

DATE1=`date -d '1 days ago' +%Y%m%d` 加-d參數可以設定與目前日期的計算時間,同樣,前2天為'2 days ago',去掉ago則為目前日期之後多少天

以下内容就是網上看到的一篇不錯的文章,出自http://www.labri.fr/perso/strandh/Teaching/USI/Common/Sh-utils/sh-utils_65.html

Here are a few examples. Also see the documentation for the `-d' option in the previous section.

To print the date of the day before yesterday:

To print the date of the day three months and one day hence:

To print the day of year of Christmas in the current year:

To print the current full month name and the day of the month:

But this may not be what you want because for the first nine days of the month, the `%d' expands to a zero-padded two-digit field, for example `date -d 1may '+%B %d'' will print `May 01'.

To print a date without the leading zero for one-digit days of the month, you can use the (GNU extension) <code>-</code> modifier to suppress the padding altogether.

To print the current date and time in the format required by many non-GNU versions of <code>date</code> when setting the system clock:

To set the system clock forward by two minutes:

To print the date in the format specified by RFC-822, use `date --rfc'. I just did and saw this:

To convert a date string to the number of seconds since the epoch (which is 1970-01-01 00:00:00 UTC), use the `--date' option with the `%s' format. That can be useful in sorting and/or graphing and/or comparing data by date. The following command outputs the number of the seconds since the epoch for the time one second later than the epoch, but in time zone five hours later (Cambridge, Massachusetts), thus a total of five hours and one second after the epoch:

Suppose you had not specified time zone information in the example above. Then, date would have used your computer's idea of the time zone when interpreting the string. Here's what you would get if you were in Greenwich, England:

If you're sorting or graphing dated data, your raw date values may be represented as seconds since the epoch. But few people can look at the date `946684800' and casually note "Oh, that's the first second of the year 2000."

To convert such an unwieldy number of seconds back to a more readable form, use a command like this:

繼續閱讀