1. date
[說明]
查看或設定時間。
[範例]
查看時間
cy@cyvm:~$ date
公曆 20十七年 一月 卅日 週一 十六時47分廿四秒
查看時間,顯示成英文格式
cy@cyvm:~$ LANG=en_US ; date
Mon Jan 30 16:53:18 CST 2017
cy@cyvm:~$ LANG=en ; date
Mon Jan 30 16:53:31 CST 2017
查看時間,格式化顯示時間
cy@cyvm:~$ date +"%Y-%m-%d %H:%M:%S"
2017-01-30 17:05:53
cy@cyvm:~$ date +"%s"
1485767170
- 一般常用年月日時分秒組合數字格式
| %F | full date; same as %Y-%m-%d |
| %T | time; same as %H:%M:%S |
| %Y | year |
| %m | month (01..12) |
| %d | day of month (e.g., 01) |
| %H | hour (00..23) |
| %M | minute (00..59) |
| %S | second (00..60) |
| %N | nanoseconds (000000000..999999999) |
- 月份文字
| %B | locale's full month name (e.g., January) |
| %b | locale's abbreviated month name (e.g., Jan) |
| %h | same as %b |
- 星期文字
| %A | locale's full weekday name (e.g., Sunday) |
| %a | locale's abbreviated weekday name (e.g., Sun) |
- 時區
| %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) |
- UNIX Timestamp (Epoch time)
| %s | seconds since 1970-01-01 00:00:00 UTC |
- 年或週的週期日數
| %j | day of year (001..366) |
| %u | day of week (1..7); 1 is Monday |
- 週數
| %U | week number of year, with Sunday as first day of week (00..53) |
| %W | week number of year, with Monday as first day of week (00..53) |
- ISO week number
| %G | year of ISO week number (see %V); normally useful only with %V |
| %g | last two digits of year of ISO week number (see %G) |
| %V | ISO week number, with Monday as first day of week (01..53) |
- 世紀
| %C | century; like %Y, except omit last two digits (e.g., 20) |
| %y | last two digits of year (00..99) |
- 12小時制
| %I | hour (01..12) |
| %p | locale's equivalent of either AM or PM; blank if not known |
| %P | like %p, but lower case |
- 地區限定格式
| %c | locale's date and time (e.g., Thu Mar 3 23:05:25 2005) |
| %x | locale's date representation (e.g., 12/31/99) |
| %X | locale's time representation (e.g., 23:13:48) |
| %r | locale's 12-hour clock time (e.g., 11:11:04 PM) |
- 特殊文字
| %% | a literal % |
| %n | a newline |
| %t | a tab |
- 不常用或有其他替代方案的格式
| %D | date; same as %m/%d/%y |
| %R | 24-hour hour and minute; same as %H:%M |
| %e | day of month, space padded; same as %_d |
| %k | hour, space padded ( 0..23); same as %_H |
| %l | hour, space padded ( 1..12); same as %_I |
| %w | day of week (0..6); 0 is Sunday |
設定時間
*注意!操作此需要 root 權限。
cy@cyvm:~$ sudo date +"%Y-%m-%d %H:%M:%S" -s "2016-10-05 12:24:36"
2016-10-05 12:24:36
2. ntpdate
[說明]
經由 NTP (Network Time Protocol,RFC 1305),設定系統時間。
[範例]
設定時間
*注意!操作此需要 root 權限。
cy@cyvm:~$ sudo ntpdate -s time.stdtime.gov.tw
3. hwclock
[說明]
在 Linux 下操作時間,一般都只是寫入作業系統,若要同步寫入硬體(RTC, real-time clock),需要使用此指令。
[範例]
查看時間
cy@cyvm:~$ sudo hwclock -r
Mon Jan 30 05:35:39 2017 .262043 seconds
設定時間
cy@cyvm:~$ sudo hwclock -w