mac date 命令使用

环境:系统 macOS Catalina 10.15.7

macOS 的 date 命令使用和 Linux 使用有稍许不同。
放上几个小例子做记录。

macOS 增减日期命令。

usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

date -v -8H '+%Y-%m-%d %H:%M:%S' # ✔️
# 2021-01-29 10:20:23
date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s" # ✔️
# 1611915623
date -j -f "%a %b %d %T %Z %Y" "Fri Jan 29 18:14:18 CST 2021" "+%Y-%m-%d %H:%M:%S" # ✔️
# 2021-01-29 18:14:18
date -v -8H -j -f "%Y-%m-%d %T" "2021-01-29 17:43:00" "+%Y-%m-%d %H:%M:%S" # ✔️
# 输出 2021-01-29 09:43:00

============================================================================================================
	-j:使用-j才能使用-f
	-n:默认情况下,如果定时进程正在运行,date命令会在本地组的所有机器设置时间。-n可以禁止这种行为,表示只设置当前计算机。
	-u:显示或者设置日期为UTC(世界协调时钟)时间
	-d:设置内核的时区,一般不使用
	-r:秒转换时间
	-t:(time zone)时区设置(GMT时区)
	-v:根据参数调整时间
		不使用+|-,更改对应的时间
		使用+:增加对应的时间
		使用-:减少对应的时间
		-f:根据格式调整时间
		+:+号引导的一些格式,和正常的Linux格式一致
============================================================================================================

-d dst  Set the kernel's value for daylight saving time.  If dst is non-zero, future calls to gettimeofday(2) will return a non-zero for tz_dsttime.

     -f      Use input_fmt as the format string to parse the new_date provided rather than using the default [[[mm]dd]HH]MM[[cc]yy][.ss] format.  Parsing is done using strptime(3).

     -j      Do not try to set the date.  This allows you to use the -f flag in addition to the + option to convert one date format to another.

     -n      By default, if the timed(8) daemon is running, date sets the time on all of the machines in the local group.  The -n option suppresses this behavior and causes the time to be set only on the current
             machine.

     -R      Use RFC 2822 date and time output format. This is equivalent to use ``%a, %d %b %Y %T %z'' as output_fmt while LC_TIME is set to the ``C'' locale .

     -r seconds
             Print the date and time represented by seconds, where seconds is the number of seconds since the Epoch (00:00:00 UTC, January 1, 1970; see time(3)), and can be specified in decimal, octal, or hex.

     -r filename
             Print the date and time of the last modification of filename.

     -t minutes_west
             Set the system's value for minutes west of GMT.  minutes_west specifies the number of minutes returned in tz_minuteswest by future calls to gettimeofday(2).

     -u      Display or set the date in UTC (Coordinated Universal) time.

     -v      Adjust (i.e., take the current date and display the result of the adjustment; not actually set the date) the second, minute, hour, month day, week day, month or year according to val.  If val is pre-
             ceded with a plus or minus sign, the date is adjusted forwards or backwards according to the remaining string, otherwise the relevant part of the date is set.  The date can be adjusted as many times
             as required using these flags.  Flags are processed in the order given.

             When setting values (rather than adjusting them), seconds are in the range 0-59, minutes are in the range 0-59, hours are in the range 0-23, month days are in the range 1-31, week days are in the
             range 0-6 (Sun-Sat), months are in the range 1-12 (Jan-Dec) and years are in the range 80-38 or 1980-2038.

             If val is numeric, one of either y, m, w, d, H, M or S must be used to specify which part of the date is to be adjusted.

             The week day or month may be specified using a name rather than a number.  If a name is used with the plus (or minus) sign, the date will be put forwards (or backwards) to the next (previous) date
             that matches the given week day or month.  This will not adjust the date, if the given week day or month is the same as the current one.

             When a date is adjusted to a specific value or in units greater than hours, daylight savings time considerations are ignored.  Adjustments in units of hours or less honor daylight saving time.  So,
             assuming the current date is March 26, 0:30 and that the DST adjustment means that the clock goes forward at 01:00 to 02:00, using -v +1H will adjust the date to March 26, 2:30.  Likewise, if the
             date is October 29, 0:30 and the DST adjustment means that the clock goes back at 02:00 to 01:00, using -v +3H will be necessary to reach October 29, 2:30.

             When the date is adjusted to a specific value that does not actually exist (for example March 26, 1:30 BST 2000 in the Europe/London timezone), the date will be silently adjusted forwards in units of
             one hour until it reaches a valid time.  When the date is adjusted to a specific value that occurs twice (for example October 29, 1:30 2000), the resulting timezone will be set so that the date
             matches the earlier of the two times.

             It is not possible to adjust a date to an invalid absolute day, so using the switches -v 31d -v 12m will simply fail five months of the year.  It is therefore usual to set the month before setting
             the day; using -v 12m -v 31d always works.

             Adjusting the date by months is inherently ambiguous because a month is a unit of variable length depending on the current date.  This kind of date adjustment is applied in the most intuitive way.
             First of all, date tries to preserve the day of the month.  If it is impossible because the target month is shorter than the present one, the last day of the target month will be the result.  For
             example, using -v +1m on May 31 will adjust the date to June 30, while using the same option on January 30 will result in the date adjusted to the last day of February.  This approach is also
             believed to make the most sense for shell scripting.  Nevertheless, be aware that going forth and back by the same number of months may take you to a different date.

             Refer to the examples below for further details.
============================================================================================================

Linux 增减日期命令。

date -d "-1 hours 2021-01-28 17:43:00" "+%Y-%m-%d %H:%M:%S"
FORMAT controls the output.  Interpreted sequences are:

       %%     a literal %

       %a     locale's abbreviated weekday name (e.g., Sun)

       %A     locale's full weekday name (e.g., Sunday)

       %b     locale's abbreviated month name (e.g., Jan)

       %B     locale's full month name (e.g., January)

       %c     locale's date and time (e.g., Thu Mar  3 23:05:25 2005)

       %C     century; like %Y, except omit last two digits (e.g., 20)

       %d     day of month (e.g., 01)

       %D     date; same as %m/%d/%y

       %e     day of month, space padded; same as %_d

       %F     full date; same as %Y-%m-%d

       %g     last two digits of year of ISO week number (see %G)

       %G     year of ISO week number (see %V); normally useful only with %V

       %h     same as %b

       %H     hour (00..23)

       %I     hour (01..12)

       %j     day of year (001..366)

       %k     hour, space padded ( 0..23); same as %_H

       %l     hour, space padded ( 1..12); same as %_I

       %m     month (01..12)

       %M     minute (00..59)

       %n     a newline

       %N     nanoseconds (000000000..999999999)

       %p     locale's equivalent of either AM or PM; blank if not known

       %P     like %p, but lower case

       %r     locale's 12-hour clock time (e.g., 11:11:04 PM)

       %R     24-hour hour and minute; same as %H:%M

       %s     seconds since 1970-01-01 00:00:00 UTC

       %S     second (00..60)

       %t     a tab

       %T     time; same as %H:%M:%S

       %u     day of week (1..7); 1 is Monday

       %U     week number of year, with Sunday as first day of week (00..53)

       %V     ISO week number, with Monday as first day of week (01..53)

       %w     day of week (0..6); 0 is Sunday

       %W     week number of year, with Monday as first day of week (00..53)

       %x     locale's date representation (e.g., 12/31/99)

       %X     locale's time representation (e.g., 23:13:48)

       %y     last two digits of year (00..99)

       %Y     year

       %z     +hhmm numeric time zone (e.g., -0400)

       %:z    +hh:mm numeric time zone (e.g., -04:00)

       %::z   +hh:mm:ss numeric time zone (e.g., -04:00:00)

       %:::z  numeric time zone with : to necessary precision (e.g., -04, +05:30)

       %Z     alphabetic time zone abbreviation (e.g., EDT)

       By default, date pads numeric fields with zeroes.  The following optional flags may follow '%':

       -      (hyphen) do not pad the field

       _      (underscore) pad with spaces

       0      (zero) pad with zeros

       ^      use upper case if possible

       #      use opposite case if possible
Logo

更多推荐