DATEDIFF(1)

๐Ÿ“ฆ
version 0.4.11 ยท 5 versions
, in alpine 3.24, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0

๐Ÿ’– DATEDIFF(1) โ€” the user commands era ๐Ÿ’–

okay bestie, sit down, hydrate, because we're about to learn datediff โ€” a User Commands icon in section 1 of the manual, and honestly? she eats. no cap.


โœจ NAME โœจ

datediff โ€” compute the duration from DATE/TIME (the reference date/time) all the way to the other one ๐Ÿ•ฐ๏ธ


โœจ SYNOPSIS โœจ

datediff [OPTION]...  DATE/TIME [DATE/TIME]...

โœจ DESCRIPTION โœจ

So here's the vibe: you compute the duration from DATE/TIME โ€” that's your reference date/time โ€” to the other DATE/TIMEs you hand over, and it prints the result as a duration. Gorgeous. If you justโ€ฆ don't give it the other DATE/TIMEs? It reads them from stdin. shy but resourceful, we love her ๐Ÿซถ

DATE/TIME can also be one of these specials, and yes they're literally magic words:

  • now โ€” interpreted as the current (UTC) time stamp
  • time โ€” the time part of the current (UTC) time stamp
  • today โ€” the current date (according to UTC)
  • tomo[rrow] โ€” tomorrow's date (according to UTC)
  • y[ester]day โ€” yesterday's date (according to UTC)
Note, bestie: the output format of durations (specified via -f) takes all format specifiers into account. i.e. specifying %M and %S for example prints the duration in minutes and seconds, whereas specifying %S only prints the duration in seconds. it's giving "read the whole string, not just one spec" ๐Ÿ’…

See section The refinement rule in datediff(1).

Recognized OPTIONs, and there are a bunch, stay with me:

  • -h, --help โ€” print help and exit. the classic.
  • -V, --version โ€” print version and exit. also the classic.
  • -q, --quiet โ€” suppress the message about date/time and duration parser errors and fix-ups. the default is to print a warning or the fixed up value and return error code 2. also see -S|--skip-illegal to output an empty line instead of leaving out the line altogether. ๐Ÿคซ
  • -S, --skip-illegal โ€” deprecated, use -E|--empty-mode. okay girl we said what we said, she's old news ๐Ÿ’€
  • -E, --empty-mode โ€” output empty lines as a placeholder for illegal input, i.e. parser errors or date/times that cannot be subtracted. very "leaving a seat empty at the table" energy.
  • -f, --format=STRING โ€” output format. this can either be a specifier string (similar to strftime()'s FMT) or the name of a calendar.
  • -i, --input-format=STRING... โ€” input format, can be used multiple times. each date/time will be passed to the input format parsers in the order they are given, and if a date/time can be read successfully with a given input format specifier string, that value will be used. first match wins, periodt.
  • -b, --base=DT โ€” for underspecified input use DT as a fallback to fill in missing fields. also used for ambiguous format specifiers to position their range on the absolute time line. must be a date/time in ISO8601 format. if omitted defaults to the current date/time. she's the safety net ๐Ÿฅบ
  • -e, --backslash-escapes โ€” enable interpretation of backslash escapes in the output and input format specifier strings.
  • --from-locale=LOCALE โ€” interpret dates on stdin or the command line as coming from the locale LOCALE; this would only affect month and weekday names as input formats have to be specified explicitly.
  • --from-zone=ZONE โ€” interpret dates on stdin or the command line as coming from the time zone ZONE.

๐ŸŒˆ FORMAT SPECS ๐ŸŒˆ

format specs in dateutils are similar to posix' strftime(). cute, right? BUT โ€” and this is the plot twist โ€” due to a broader range of supported calendars, dateutils must employ different rules. so don't come in here assuming, bestie.

Date specs ๐Ÿ“…

  • %a โ€” the abbreviated weekday name
  • %A โ€” the full weekday name
  • %_a โ€” the weekday name shortened to a single character (MTWRFAS)
  • %b โ€” the abbreviated month name
  • %B โ€” the full month name
  • %_b โ€” the month name shortened to a single character (FGHJKMNQUVXZ)
  • %c โ€” the count of the weekday within the month (range 00 to 05)
  • %C โ€” the count of the weekday within the year (range 00 to 53)
  • %d โ€” the day of the month, 2 digits (range 00 to 31)
  • %D โ€” the day of the year, 3 digits (range 000 to 366)
  • %F โ€” equivalent to %Y-%m-%d (ymd's canonical format)
  • %g โ€” ISO week date year without the century (range 00 to 99)
  • %G โ€” ISO week date year including the century
  • %j โ€” equivalent to %D
  • %m โ€” the month in the current calendar (range 00 to 12)
  • %Q โ€” the quarter of the year (range Q1 to Q4)
  • %q โ€” the number of the quarter (range 01 to 04)
  • %s โ€” the number of seconds since the Epoch.
  • %u โ€” the weekday as number (range 01 to 07, Sunday being 07)
  • %U โ€” the week count, day of week is Sun (range 00 to 53)
  • %V โ€” the ISO week count, day of week is Mon (range 01 to 53)
  • %w โ€” the weekday as number (range 00 to 06, Sunday being 00)
  • %W โ€” the week count, day of week is Mon (range 00 to 53)
  • %y โ€” the year without a century (range 00 to 99)
  • %Y โ€” the year including the century
  • %_y โ€” the year shortened to a single digit
  • %Z โ€” the zone offset in hours and minutes (HH:MM) with a preceding sign (+ for offsets east of UTC, - for offsets west of UTC)
  • %Od โ€” the day as roman numerals
  • %Om โ€” the month as roman numerals
  • %Oy โ€” the two digit year as roman numerals
  • %OY โ€” the year including the century as roman numerals
  • %rs โ€” in time systems whose Epoch is different from the unix Epoch, this selects the number of seconds since then.
  • %rY โ€” in calendars with years that don't coincide with the Gregorian years, this selects the calendar's year.
  • %dth โ€” the day of the month as an ordinal number, 1st, 2nd, 3rd, etc.
  • %mth โ€” the month of the year as an ordinal number, 1st, 2nd, 3rd, etc.
  • %db โ€” the business day of the month (since last month's ultimo)
  • %dB โ€” number of business days until this month's ultimo

Time specs โฐ

  • %H โ€” the hour of the day using a 24h clock, 2 digits (range 00 to 23)
  • %I โ€” the hour of the day using a 12h clock, 2 digits (range 01 to 12)
  • %M โ€” the minute (range 00 to 59)
  • %N โ€” the nanoseconds (range 000000000 to 999999999)
  • %p โ€” the string AM or PM, noon is PM and midnight is AM.
  • %P โ€” like %p but in lowercase
  • %S โ€” the seconds (range 00 to 60, 60 is for leap seconds)
  • %T โ€” equivalent to %H:%M:%S

General specs ๐ŸŽ€

  • %n โ€” a newline character
  • %t โ€” a tab character
  • %% โ€” a literal % character

Modifiers ๐Ÿฆ‹

  • %O โ€” modifier to turn decimal numbers into Roman numerals
  • %r โ€” modifier to turn units into real units
  • %0 โ€” modifier to turn on zero prefixes
  • %SPC โ€” modifier to turn on space prefixes
  • %- โ€” modifier to turn off prefixes altogether
  • th โ€” suffix, read and print ordinal numbers
  • b โ€” suffix, treat days as business days
By design dates before 1601-01-01 are not supported. like, at all. don't try it.

For conformity here is a list of calendar designators and their corresponding format string:

ymd     %Y-%m-%d
ymcw    %Y-%m-%c-%w
ywd     %rY-W%V-%u
bizda   %Y-%m-%db
lilian     n/a
ldn        n/a
julian     n/a
jdn        n/a
matlab     n/a
mdn        n/a

These designators can be used as output format string, moreover, lilian/ldn and julian/jdn can also be used as input format string. so those four get to be in the input club too, iconic ๐Ÿ‘‘


๐ŸŒธ FORMAT SPECS FOR DURATIONS ๐ŸŒธ

unlike time or absolute instants, durations are reference-free, i.e. the reference instant is not part of the duration. as a result, durations cannot be named, i.e. there is no naming scheme that applies to all durations and all references unambiguously. she's a free spirit, she answers to no name ๐Ÿ˜Œ

consequently, none of the format specifiers for date/times makes sense for durations in the literal sense. however โ€” and this is the sweet part โ€” to aid intuitive usage we reused format specifiers when they represent integral values and a valid unit for duration, as follows:

Date specs ๐Ÿ“†

  • %c โ€” equivalent to %w
  • %d โ€” duration in days
  • %F โ€” equivalent to %dd with no resorting to bigger units
  • %m โ€” duration in months
  • %w โ€” duration in weeks
  • %y โ€” equivalent to %Y
  • %Y โ€” duration in years
  • %db โ€” duration in business days
  • %dB โ€” equivalent to %db

Time specs โณ

  • %H โ€” duration in hours
  • %I โ€” equivalent to %H
  • %M โ€” duration in minutes
  • %S โ€” duration in seconds
  • %T โ€” equivalent to %Ss without resorting to bigger units
  • %rS โ€” duration in real-life seconds, as in including leap seconds
  • %rT โ€” equivalent to %rSs without resorting to bigger units

General specs โœจ

  • %n โ€” a newline character
  • %t โ€” a tab character
  • %% โ€” a literal % character

Modifiers ๐Ÿ’…

  • %r โ€” modifier to turn units into real units
  • %0 โ€” modifier to pad refined values with zeroes
  • %SPC โ€” modifier to pad refined values with spaces
  • b โ€” suffix, treat days as business days

๐Ÿ‘‘ THE REFINEMENT RULE ๐Ÿ‘‘

okay this is the big one, the main character section, listen up.

durations are somewhat ambiguous when it comes to representing them through format specifiers. unlike format specifiers in point-in-time representations, duration specifiers can have an intra-line relationship. messy? a little. slay? absolutely.

so for instance a duration of 128 seconds might be presented through "%S" as "128" but similarly through "%M:%S" as "02:08" (read two minutes and 8 seconds). same duration, two fits, both cute.

there are several approaches to deal with this ambiguity. the datediff tool will follow what we call the refinement rule. that is, regardless of the position of a format specifier, if it is a valid /refinement/ of another specifier in the format string, then it will only show the fractional value, i.e. the value in its natural range with respect to the /refined/ specifier.

%Y  possible refinements: %m, %w, %d
%m  possible refinements: %w, %d
%w  possible refinements: %d
%d  possible refinements: %H, %M, %S
%H  possible refinements: %M, %S
%M  possible refinements: %S

the refinement alternatives are listed in order of precedence and they are mutually exclusive. i.e. it is not possible to express a duration in months and hours without having a "%d" specifier as well. on the other hand, in a chain of refinements inner elements are optional, i.e. you can express a duration in weeks and hours because every day has 24 hours and hence there are 168 hours in a week. math ate, honestly ๐Ÿงฎ

in case of negative durations (the minuend is in the future relative to the subtrahend) only the largest unit will carry the minus sign. one minus sign, no drama.

using the refinement rule keeps the format string dead simple โ€” there's no need for operators or a full-blown language to distinguish the range ambiguity, which then would have to be escaped because they could also in theory be part of the literal characters of the format string, resulting more often than not in command lines that are hard to craft and even harder to understand later on (e.g. if used in shell scripts). and we are not doing unreadable shell scripts in this house ๐Ÿ™ˆ

the refinement rule ingeniously covers the 99% case but, unlike other approaches, there's no way to display two unrefined values in the same format string, e.g. "'%w weeks (which is %d days)'". that's the tradeoff, bestie, and it's lowkey worth it.

โœจ EXAMPLES โœจ

okay bestie, this is the part where it all clicks ๐Ÿ’… every single one of these is copy-pasteable, so go wild.

Same date twice? Zero. Obviously. The math ate and left no crumbs:

$ datediff 2012-03-02 2012-03-02
0
$

Ten days apart, and it just says 10. no units, no drama:

$ datediff 2012-03-02 2012-03-12
10
$

Forty-one days. spring break era:

$ datediff 2012-03-02 2012-04-12
41
$

Crossing a month boundary? Still chill, still just days:

$ datediff 2012-03-12 2012-04-02
21
$

And if you flip them around you get a negative number, because the order you put them in literally matters:

$ datediff 2012-04-02 2012-03-12
-21
$

now let's get โœจfancyโœจ with format specs. 2012 is a leap year so that's 58 days, and the -f flag makes it say the d out loud:

$ datediff 2012-01-02 2012-02-29 -f '%dd'
58d
$

Weeks AND days? We love a combo platter ๐Ÿ“:

$ datediff 2012-01-02 2012-02-29 -f '%ww %dd'
8w 2d
$

Times of day! Same time = zero seconds, no notes:

$ datediff 10:00:00 10:00:00
0s
$

Five minutes is 300 seconds, and yes it defaults to seconds, that's the whole vibe:

$ datediff 10:01:00 10:06:00
300s
$

Backwards time travel gets you the minus sign again ๐Ÿ™ˆ:

$ datediff 10:06:00 10:01:00
-300s
$

now we're just showing off with the format specs. seconds, spelled out:

$ datediff 10:01:00 11:03:10 -f '%S sec'
3730 sec
$

minutes and seconds, bestie style:

$ datediff 10:01:00 11:03:10 -f '%Mm %Ss'
62m 10s
$

the full clock look, and notice 1:2:10 โ€” no zero-padding, she said minimal effort, maximal slay:

$ datediff 10:01:00 11:03:10 -f '%H:%M:%S'
1:2:10
$

Full ISO timestamps? The T goes in, the math comes out. 10 minutes = 600 seconds:

$ datediff 2012-03-02T10:04:00 2012-03-02T10:14:00
600s
$

and with a lil format spec it says 10 min like a person would:

$ datediff 2012-03-02T10:04:00 2012-03-02T10:14:00 -f '%M min'
10 min
$

overnight! 92580s because it counted every single one of those seconds, no shortcuts:

$ datediff 2012-03-01T12:17:00 2012-03-02T14:00:00
92580s
$

and the final boss โ€” mixing day units and second units in ONE format string. note that %d gives days and %S gives the remaining seconds, so 6180 seconds is what's left after that one whole day:

$ datediff 2012-03-01T12:17:00 2012-03-02T14:00:00 -f '%d days and %S seconds'
1 days and 6180 seconds
$

(yeah it says 1 days. she's not fixing that, that's just the format spec doing exactly what you told it to do. ๐Ÿ’€)


โœ๏ธ AUTHOR

Written by Sebastian Freundt <freundt@fresse.org> โ€” absolute icon, no cap ๐Ÿ‘‘


๐Ÿ› REPORTING BUGS

Found something cursed? Report bugs to: https://github.com/hroptatyr/dateutils/issues

bestie, they want to hear it. go off. ๐Ÿซถ


๐Ÿ‘€ SEE ALSO

The full documentation for datediff is maintained as a Texinfo manual. If the info and datediff programs are properly installed at your site, the command

info (dateutils)datediff

should give you access to the complete manual. iykyk โ€” the texinfo version is the extended cut ๐Ÿ’–


and that's the whole page, bestie!! ๐ŸŒˆ this was dateutils 0.4.11, dated January 2024, and yes you just read DATEDIFF(1) cover to cover like the main character you are. go format some dates. slay. ๐Ÿ’…โœจ