Linux date-d parameter usage

Source: Internet
Author: User
Tags local time month name rfc

Lately I've racked my brains to write a shell that adjusts time, and it turns out that someday
#info data
It was ecstatic to see the flexible use of the data-d parameter. Good things to keep, tidy up:

* To print the date of the day before yesterday:

Date--date= ' 2 days ago '

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

Date--date= ' 3 months 1 day '

* To print the day's of Christmas in the current year:

Date--date= ' Dec ' +%j

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

Date ' +%b%d '

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 ' would print ' May 01 '.

* To print a date without the leading zero for one-digit days of the
month, you can use the (GNU extension) '-' flag to suppress the
Padding altogether:

Date-d 1may ' +%b%-d

* To print the current date and time of the format required by many
Non-gnu versions of ' Date ' when setting the system clock:

Date +%m%d%h%m%y.%s

* To set the system clock forward by minutes:

Date--set= ' +2 minutes '

* To-print the date in RFC 2822 format with use ' Date--rfc-2822 '. Here
Is some example output:

Fri, Sep 2005 13:51:39-0700

* 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 is 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 and the Minutes
After the epoch:

Date--date= ' 1970-01-01 00:02:00 +0000 ' +%s

Date--date= ' 1970-01-01 00:02:00 +0000 ' +%s
120

If You don't specify time zone information in the date string,
' Date ' uses your computer ' s idea of the time zone when
Interpreting the string. For example, if your computer ' s time
Zone is that's Cambridge, Massachusetts, which was and then 5 hours
(i.e., 18,000 seconds) behind UTC:

# local time zone used
Date--date= ' 1970-01-01 00:02:00 ' +%s
18120

* 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 in Greenwich, England. "

Date--date= ' 2000-01-01 UTC ' +%s
946684800

An alternative are to use the '--UTC ' ('-u ') option. Then, May
Omit ' UTC ' from the date string. Although this produces the same
Result for '%s ' and many other format sequences, with a time zone
Offset different from zero, it would give a different result for
Zone-dependent formats like '%z '.

Date-u--date=2000-01-01 +%s
946684800

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

# local time zone used
Date-d ' 1970-01-01 UTC 946684800 seconds ' + "%y-%m-%d%T%z"
1999-12-31 19:00:00-0500

Often It is better to output utc-relative date and time:

Date-u-d ' 1970-01-01 946684800 seconds ' + "%y-%m-%d%T%z"
2000-01-01 00:00:00 +0000

Linux date-d parameter usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.