datemath
Manipulate a UNIX time by adding and subtracting seconds, minutes, hours, days, and weeks allowing flexible control of output format (like the date command, but able to output different times than the present).
datemath [time] [date] [hour] [noon] [midnight] [now] [Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec] [firstday | lastday | Sun | Mon | Tue | Wed | Thu | Fri | Sat] [+|-ns] [+|-nm] [+|-nh] [+|-nd] [+|-nw] [+|-nM] [+|-nQ] [+|-nY] [-f format] [-F filename position]
The datemath command performs math on the date and time and allows you to output the result with most of the options of the date command. This becomes particularly useful when you need to know yesterday's date, tomorrow's date, or some other relative date or time. By default, datemath starts with the current date and time, however the first parameter may be a fixed date and time (specified as seconds since Jan 1, 1970). From there, specify as many manipulations as needed, for example +1d to move forward 1 day or -1h to move back 1 hour.
The resulting time value is output as seconds since Jan 1, 1970 unless another format is specified. The format follows the -f command line option an generally follows the options available in the date command. For example,
datemath -1d -f "%D"
will output yesterday's date. See the formatting substitutions below.
Beware that the order of arguments matters. For example,
datemath Mar lastday -f "%D"
will output the date for the last day of March (03/31/2000), whereas:
datemath lastday Mar -f "%D"
will output the day in March corresponding to the last day of the current month (03/30/2000 if run in April).
Option |
Description | ||||||||||||||||||||||||||||||||
time |
The time as seconds since Jan 1, 1970 to specify as the base time to manipulate. | ||||||||||||||||||||||||||||||||
date |
The date (month/day/year) of the base time to manipulate, e.g. 10/2/96. Hours, minutes, and seconds are untouched. | ||||||||||||||||||||||||||||||||
hour |
The hour (hour:minute:seconds or hour:minutes) of the base time to manipulate, e.g. 12:40. Month, day, and year are untouched. | ||||||||||||||||||||||||||||||||
noon |
Set the hour of the base time to manipulate to 12:00 noon. Month, day, and year are untouched. | ||||||||||||||||||||||||||||||||
midnight |
Set the hour of the base time to manipulate to 12:00 midnight. Month, day, and year are untouched. | ||||||||||||||||||||||||||||||||
now |
Set the hour of the base time to manipulate to right now. This serves as a place holder since datemath by default defaults to right now. | ||||||||||||||||||||||||||||||||
Jan |
Set the month to January. If the month is not January, then the previous January is used. Time of day remains untouched. | ||||||||||||||||||||||||||||||||
Feb |
Set the month to February. If the month is not February, then the previous February is used. Time of day remains untouched. | ||||||||||||||||||||||||||||||||
Mar |
Set the month to March. If the month is not March, then the previous March is used. Time of day remains untouched. | ||||||||||||||||||||||||||||||||
Apr |
Set the month to April. If the month is not April, then the previous April is used. Time of day remains untouched. | ||||||||||||||||||||||||||||||||
May |
Set the month to May. If the month is not May, then the previous May is used. Time of day remains untouched. | ||||||||||||||||||||||||||||||||
Jun |
Set the month to June. If the month is not June, then the previous June is used. Time of day remains untouched. | ||||||||||||||||||||||||||||||||
Jul |
Set the month to July. If the month is not July, then the previous July is used. Time of day remains untouched. | ||||||||||||||||||||||||||||||||
Aug |
Set the month to August. If the month is not August, then the previous August is used. Time of day remains untouched. | ||||||||||||||||||||||||||||||||
Sep |
Set the month to September. If the month is not September, then the previous September is used. Time of day remains untouched. | ||||||||||||||||||||||||||||||||
Oct |
Set the month to October. If the month is not October, then the previous October is used. Time of day remains untouched. | ||||||||||||||||||||||||||||||||
Nov |
Set the month to November. If the month is not November, then the previous November is used. Time of day remains untouched. | ||||||||||||||||||||||||||||||||
Dec |
Set the month to December. If the month is not December, then the previous December is used. Time of day remains untouched. | ||||||||||||||||||||||||||||||||
firstday |
Set the day of the month to the first of the month. Month, year, and time untouched. | ||||||||||||||||||||||||||||||||
lastday |
Set the day of the month to the last of the month. Month, year, and time untouched. | ||||||||||||||||||||||||||||||||
Sun |
Set the day of the week to Sunday. If the day is not Sunday, then the previous Sunday is used. Time of day remains untouched. | ||||||||||||||||||||||||||||||||
Mon |
Set the day of the week to Monday. If the day is not Monday, then the previous Monday is used. Time of day remains untouched. | ||||||||||||||||||||||||||||||||
Tue |
Set the day of the week to Tuesday. If the day is not Tuesday, then the previous Tuesday is used. Time of day remains untouched. | ||||||||||||||||||||||||||||||||
Wed |
Set the day of the week to Wednesday. If the day is not Wednesday, then the previous Wednesday is used. Time of day remains untouched. | ||||||||||||||||||||||||||||||||
Thu |
Set the day of the week to Thursday. If the day is not Thursday, then the previous Thursday is used. Time of day remains untouched. | ||||||||||||||||||||||||||||||||
Fri |
Set the day of the week to Friday. If the day is not Friday, then the previous Friday is used. Time of day remains untouched. | ||||||||||||||||||||||||||||||||
Sat |
Set the day of the week to Saturday. If the day is not Saturday, then the previous Saturday is used. Time of day remains untouched. | ||||||||||||||||||||||||||||||||
[+|-]n s |
Add (+) or subtract - n seconds of time. | ||||||||||||||||||||||||||||||||
[+|-]n m |
Add (+) or subtract - n minutes of time. | ||||||||||||||||||||||||||||||||
[+|-]n h |
Add (+) or subtract - n hours of time. | ||||||||||||||||||||||||||||||||
[+|-]n d |
Add (+) or subtract - n days of time. | ||||||||||||||||||||||||||||||||
[+|-]n w |
Add (+) or subtract - n weeks of time. | ||||||||||||||||||||||||||||||||
[+|-]n M |
Add (+) or subtract - n months of time. Be careful adding time to the end of a month. For example, adding 1 month to Aug 31 does not produce Sep 31, but Oct 1. | ||||||||||||||||||||||||||||||||
[+|-]n Q |
Add (+) or subtract - n quarters (3 months) of time. | ||||||||||||||||||||||||||||||||
[+|-]n Y |
Add (+) or subtract - n years of time. | ||||||||||||||||||||||||||||||||
-f format |
Specify format of output. The format can include any text as well as specifal substitions that start with %. For example, "Date: %D" will output Date: 05/06/96 if the resulting date was May 6, 1996. The following table shows you what substitutions are allowed:
| ||||||||||||||||||||||||||||||||
-F filename position |
Use this command line option to translate dates in a file (use - for stdin) from the Unix format (seconds since Jan 1, 1970) to the format specified. The position of the Unix formatted time must be specified as either a token on the line or as byte positions. Tokens on a line range from 1 upward. Byte positions are specified as a range: lower-upper where byte positions start at 1 for the first character on the line. |