DATEDIFF(1)
๐ 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 stamptimeโ the time part of the current (UTC) time stamptodayโ 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%Mand%Sfor example prints the duration in minutes and seconds, whereas specifying%Sonly 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 code2. also see-S|--skip-illegalto 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 tostrftime()'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 (range00to05)%Cโ the count of the weekday within the year (range00to53)%dโ the day of the month, 2 digits (range00to31)%Dโ the day of the year, 3 digits (range000to366)%Fโ equivalent to%Y-%m-%d(ymd's canonical format)%gโ ISO week date year without the century (range00to99)%Gโ ISO week date year including the century%jโ equivalent to%D%mโ the month in the current calendar (range00to12)%Qโ the quarter of the year (rangeQ1toQ4)%qโ the number of the quarter (range01to04)%sโ the number of seconds since the Epoch.%uโ the weekday as number (range01to07, Sunday being07)%Uโ the week count, day of week is Sun (range00to53)%Vโ the ISO week count, day of week is Mon (range01to53)%wโ the weekday as number (range00to06, Sunday being00)%Wโ the week count, day of week is Mon (range00to53)%yโ the year without a century (range00to99)%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 (range00to23)%Iโ the hour of the day using a 12h clock, 2 digits (range01to12)%Mโ the minute (range00to59)%Nโ the nanoseconds (range000000000to999999999)%pโ the stringAMorPM, noon is PM and midnight is AM.%Pโ like%pbut in lowercase%Sโ the seconds (range00to60,60is 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 altogetherthโ suffix, read and print ordinal numbersbโ 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%ddwith 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%Sswithout resorting to bigger units%rSโ duration in real-life seconds, as in including leap seconds%rTโ equivalent to%rSswithout 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 spacesbโ 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. ๐
โจ
๐ฆ where this page comes from
- alpine 3.24: dateutils-doc 0.4.11-r0 ยท BSD-3-Clause
- fedora 43: dateutils 0.4.11-5.fc43 ยท BSD-3-Clause
- fedora 44: dateutils 0.4.11-7.fc44 ยท BSD-3-Clause
- nixos 26.05: dateutils 0.4.11
- nixpkgs unstable: dateutils 0.4.11
- opensuse-leap 16.0: dateutils 0.4.11-bp160.1.17 ยท BSD-3-Clause
- opensuse-leap 16.0: dateutils 0.4.11-bp160.1.17 ยท BSD-3-Clause
- opensuse-leap 16.0: dateutils 0.4.11-bp160.1.17 ยท BSD-3-Clause
- opensuse-leap 16.0: dateutils 0.4.11-bp160.1.17 ยท BSD-3-Clause
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms