Why does date +%Y%m%d -s "20081127" set the date to 2008 (year), 11 (month) and 27 day, but date +%Y -s "2008" sets the clock to 20:08?


Based on the presence of the -s flag, I'm assuming that you describe the GNU date command (part of the GNU coreutils set of utilities).

The format string only affects the output of date. The utility does not use the supplied format string to parse the given date/time string.

GNU date has quite complex rules for parsing dates. You may find these in the info date or info coreutils documentation under the "Date input formats" heading.

Especially the text under "Pure numbers in date strings" would be interesting to you:

The precise interpretation of a pure decimal number depends on the context in the date string.

If the decimal number is of the form yyyymmdd and no other calendar date item (see Calendar date items) appears before it in the date string, then yyyy is read as the year, mm as the month number and dd as the day of the month, for the specified calendar date.

If the decimal number is of the form hhmm and no other time of day item appears before it in the date string, then hh is read as the hour of the day and mm as the minute of the hour, for the specified time of day. mm can also be omitted.

If both a calendar date and a time of day appear to the left of a number in the date string, but no relative item, then the number overrides the year.

Note that this applies to all utilities in the GNU coreutils collection that are able to parse dates, not just GNU date.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.