mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-22 23:00:57 +08:00
TSL
This commit is contained in:
parent
ea467621f3
commit
83643e017f
@ -1,180 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (wxy)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Displaying dates and times your way)
|
||||
[#]: via: (https://www.networkworld.com/article/3481602/displaying-dates-and-times-your-way-with-linux.html)
|
||||
[#]: author: (Sandra Henry-Stocker https://www.networkworld.com/author/Sandra-Henry_Stocker/)
|
||||
|
||||
Displaying dates and times your way
|
||||
======
|
||||
The Linux date command provides more options for displaying dates and times than you can shake a stick at (without hurting your wrist anyway). Here are some of the more useful choices.
|
||||
Thinkstock / Tomislav Jakupec
|
||||
|
||||
The date command on Linux systems is very straightforward. You type “date” and the date and time are displayed in a useful way. It includes the day-of-the-week, calendar date, time and time zone:
|
||||
|
||||
```
|
||||
$ date
|
||||
Tue 26 Nov 2019 11:45:11 AM EST
|
||||
```
|
||||
|
||||
As long as your system is configured properly, you’ll see the date and current time along with your time zone.
|
||||
|
||||
[[Get regularly scheduled insights by signing up for Network World newsletters.]][1]
|
||||
|
||||
The command, however, also offers a lot of options to display date and time information differently. For example, if you want to display dates in the most useful format for sorting, you might want to use a command like this:
|
||||
|
||||
[][2]
|
||||
|
||||
BrandPost Sponsored by HPE
|
||||
|
||||
[Take the Intelligent Route with Consumption-Based Storage][2]
|
||||
|
||||
Combine the agility and economics of HPE storage with HPE GreenLake and run your IT department with efficiency.
|
||||
|
||||
```
|
||||
$ date "+%Y-%m-%d"
|
||||
2019-11-26
|
||||
```
|
||||
|
||||
In this case, the year, month and day are arranged in that order. Note that we use a capital Y to get a four-digit year. If we use a lowercase y, we’d see only a two-digit year (e.g., 19). Don’t let this induce you into thinking that if %m gives you a numeric month, **%**M might give you the name of the month. No, **%**M will report on minutes. To get the month in abbreviated name format, you would use **%**b and for a fully spelled out month, you would use **%**B.
|
||||
|
||||
```
|
||||
$ date "+%b %B"
|
||||
Nov November
|
||||
```
|
||||
|
||||
Alternately, you might want to display the date in this commonly used format:
|
||||
|
||||
```
|
||||
$ date +%D
|
||||
11/26/19
|
||||
```
|
||||
|
||||
If you need a four-digit year, you can do this:
|
||||
|
||||
```
|
||||
$ date "+%x"
|
||||
11/26/2019
|
||||
```
|
||||
|
||||
Here’s an example that might be useful. Say that you need to create a daily report and have the file name include the date, you could use a command like this to create the file – probably in a script:
|
||||
|
||||
```
|
||||
touch Report-`date "+%Y-%m-%d"`
|
||||
```
|
||||
|
||||
When you list your reports, they’ll list in date order or reverse date order if you add -r.
|
||||
|
||||
```
|
||||
$ ls -r Report*
|
||||
Report-2019-11-26
|
||||
Report-2019-11-25
|
||||
Report-2019-11-22
|
||||
Report-2019-11-21
|
||||
Report-2019-11-20
|
||||
```
|
||||
|
||||
You can add other details to your date strings as well. The variety of options available is surprising. You could show which quarter of the year you’re in by using **date "+%q"** or display the date it was two months ago with a command like this:
|
||||
|
||||
```
|
||||
$ date --date="2 months ago"
|
||||
Thu 26 Sep 2019 09:02:43 AM EDT
|
||||
```
|
||||
|
||||
Want to see what next Thursday’s date will be? You can use a command like **date --date="next thu"**, but understand that, for Linux, next Thursday means whatever Thursday follows today. That’s tomorrow if today is Wednesday – not Thursday of next week. However, you can specify Thursday of next week as in the second command below.
|
||||
|
||||
```
|
||||
$ date --date="next thu"
|
||||
Thu 28 Nov 2019 12:00:00 AM EST
|
||||
$ date --date="next week thu"
|
||||
Thu 05 Dec 2019 12:00:00 AM EST
|
||||
```
|
||||
|
||||
The man page for the date command lists all of its options. The list is fairly mind boggling, but you’ll probably find some date/time display options that work really well for you. Here are some that you might find interesting.
|
||||
|
||||
The date in universal time (UTC):
|
||||
|
||||
```
|
||||
$ date -u
|
||||
Tue 26 Nov 2019 01:13:59 PM UTC
|
||||
```
|
||||
|
||||
The number of seconds since Jan 1, 1970 (related to how dates are stored on Linux systems):
|
||||
|
||||
```
|
||||
$ date +%s
|
||||
1574774137
|
||||
```
|
||||
|
||||
Here's a full listing of the date command's options. As I said, it's a lot more extensive than most of us likely imagine.
|
||||
|
||||
```
|
||||
%% 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
|
||||
%q quarter of year (1..4)
|
||||
%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)
|
||||
```
|
||||
|
||||
Join the Network World communities on [Facebook][3] and [LinkedIn][4] to comment on topics that are top of mind.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.networkworld.com/article/3481602/displaying-dates-and-times-your-way-with-linux.html
|
||||
|
||||
作者:[Sandra Henry-Stocker][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://www.networkworld.com/author/Sandra-Henry_Stocker/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://www.networkworld.com/newsletters/signup.html
|
||||
[2]: https://www.networkworld.com/article/3440100/take-the-intelligent-route-with-consumption-based-storage.html?utm_source=IDG&utm_medium=promotions&utm_campaign=HPE20773&utm_content=sidebar ( Take the Intelligent Route with Consumption-Based Storage)
|
||||
[3]: https://www.facebook.com/NetworkWorld/
|
||||
[4]: https://www.linkedin.com/company/network-world
|
166
translated/tech/20191127 Displaying dates and times your way.md
Normal file
166
translated/tech/20191127 Displaying dates and times your way.md
Normal file
@ -0,0 +1,166 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (wxy)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Displaying dates and times your way)
|
||||
[#]: via: (https://www.networkworld.com/article/3481602/displaying-dates-and-times-your-way-with-linux.html)
|
||||
[#]: author: (Sandra Henry-Stocker https://www.networkworld.com/author/Sandra-Henry_Stocker/)
|
||||
|
||||
按你的方式显示日期和时间
|
||||
======
|
||||
|
||||
> Linux date 命令提供了很多显示日期和时间的选项,要比你想的还要多。这是一些更有用的选择。
|
||||
|
||||
在 Linux 系统上,`date` 命令非常简单。你键入 `date`,日期和时间将以一种有用的方式显示。它包括星期几、日期、时间和时区:
|
||||
|
||||
```
|
||||
$ date
|
||||
Tue 26 Nov 2019 11:45:11 AM EST
|
||||
```
|
||||
|
||||
只要你的系统配置正确,你就会看到日期和当前时间以及时区。
|
||||
|
||||
但是,该命令还提供了许多选项来以不同方式显示日期和时间信息。例如,如果要显示日期以进行排序,则可能需要使用如下命令:
|
||||
|
||||
```
|
||||
$ date "+%Y-%m-%d"
|
||||
2019-11-26
|
||||
```
|
||||
|
||||
在这种情况下,年、月和日按该顺序排列。请注意,我们使用大写字母 `Y` 来获得四位数的年份。如果我们使用小写的 `y`,则只会看到两位数字的年份(例如 19)。不要让这种想法使你联想到,如果 `%m` 给你一个数字月份,`%M` 可能会给你月份的名称。不,`%M` 将给你分钟数。要以缩写名称格式获得月份,你要使用 `%b`,而对于完全拼写的月份,则要使用 `%B`。
|
||||
|
||||
```
|
||||
$ date "+%b %B"
|
||||
Nov November
|
||||
```
|
||||
|
||||
或者,你可能希望以这种常用格式显示日期:
|
||||
|
||||
```
|
||||
$ date +%D
|
||||
11/26/19
|
||||
```
|
||||
|
||||
如果你需要四位数的年份,则可以执行以下操作:
|
||||
|
||||
```
|
||||
$ date "+%x"
|
||||
11/26/2019
|
||||
```
|
||||
|
||||
下面是一个可能有用的示例。假设你需要创建一个每日报告并在文件名中包含日期,则可以使用以下命令来创建文件(可能用在脚本中):
|
||||
|
||||
```
|
||||
$ touch Report-`date "+%Y-%m-%d"`
|
||||
```
|
||||
|
||||
当你列出你的报告时,它们将按日期顺序或反向日期顺序(如果你添加 `-r`)列出。
|
||||
|
||||
```
|
||||
$ ls -r Report*
|
||||
Report-2019-11-26
|
||||
Report-2019-11-25
|
||||
Report-2019-11-22
|
||||
Report-2019-11-21
|
||||
Report-2019-11-20
|
||||
```
|
||||
|
||||
你还可以在日期字符串中添加其他详细信息。可用的各种选项令人惊讶。你可以使用 `date "+%q"` 来显示你所在的一年中的哪个季度,或使用类似以下命令来显示两个月前的日期:
|
||||
|
||||
```
|
||||
$ date --date="2 months ago"
|
||||
Thu 26 Sep 2019 09:02:43 AM EDT
|
||||
```
|
||||
|
||||
是否想知道下周四的日期?你可以使用类似 `date --date="next thu"` 的命令,但是要理解,对于Linux,下个周四意味着今天之后的周四。如果今天是星期三,那就是明天,而不是下周的星期四。但是,你可以像下面的第二个命令一样指定下周的星期四。
|
||||
|
||||
```
|
||||
$ date --date="next thu"
|
||||
Thu 28 Nov 2019 12:00:00 AM EST
|
||||
$ date --date="next week thu"
|
||||
Thu 05 Dec 2019 12:00:00 AM EST
|
||||
```
|
||||
|
||||
`date` 命令的手册页列出了其所有选项。该列表令人难以置信,但是你可能会发现一些日期/时间显示选项非常适合您。以下是一些你可能会发现有趣的东西。
|
||||
|
||||
世界标准时间(UTC):
|
||||
|
||||
```
|
||||
$ date -u
|
||||
Tue 26 Nov 2019 01:13:59 PM UTC
|
||||
```
|
||||
|
||||
自 1970 年 1 月 1 日以来的秒数(与 Linux 系统上日期的存储方式有关):
|
||||
|
||||
```
|
||||
$ date +%s
|
||||
1574774137
|
||||
```
|
||||
|
||||
这是 `date` 命令选项的完整列表。正如我所说,它比我们大多数人想象的要广泛得多。
|
||||
|
||||
- `%%` 字母 %
|
||||
- `%a` 语言环境的缩写星期名称(例如,日 / Sun)
|
||||
- `%A` 语言环境的完整星期名称(例如,星期日 / Sunday)
|
||||
- `%b` 语言环境的缩写月份名称(例如 一 / Jan)
|
||||
- `%B` 语言环境的完整月份名称(例如,一月 / January)
|
||||
- `%c` 语言环境的日期和时间(例如 2005年3月3日 星期四 23:05:25 / Thu Mar 3 23:05:25 2005)
|
||||
- `%C` 世纪;类似于 `%Y`,但省略了后两位数字(例如,20)
|
||||
- `%d` 月份的天(例如,01)
|
||||
- `%D` 日期;与 `%m/%d/%y` 相同
|
||||
- `%e` 月份的天,填充前缀空格;与 `%_d` 相同
|
||||
- `%F` 完整日期;与 `%Y-%m-%d` 相同
|
||||
- `%g` ISO 周号的年份的后两位数字(请参见 `%G`)
|
||||
- `%G` ISO 周号的年份(请参阅 `%V`);通常仅配合 `%V`有用
|
||||
- `%h` 与 `%b` 相同
|
||||
- `%H` 小时(00..23)
|
||||
- `%I` 小时(01..12)
|
||||
- `%j` 一年的天(001..366)
|
||||
- `%k` 小时,填充前缀空格( 0..23);与 `%_H` 相同
|
||||
- `%l` 小时,填充前缀空格( 1..12);与 `%_I` 相同
|
||||
- `%m` 月份(01..12)
|
||||
- `%M` 分钟(00..59)
|
||||
- `%n` 换行符
|
||||
- `%N` 纳秒(000000000..999999999)
|
||||
- `%p` 语言环境中等同于 AM 或 PM 的字符串;如果未知,则为空白
|
||||
- `%P` 像 `%p`,但使用小写
|
||||
- `%q` 季度(1..4)
|
||||
- `%r` 语言环境的 12 小时制时间(例如,晚上 11:11:04 / 11:11:04 PM)
|
||||
- `%R` 24 小时制的小时和分钟;与 `%H:%M` 相同
|
||||
- `%s` 自 1970-01-01 00:00:00 UTC 以来的秒数
|
||||
- `%S` 秒(00..60)
|
||||
- `%t` 制表符
|
||||
- `%T` 时间;与 `%H:%M:%S` 相同
|
||||
- `%u` 星期(1..7);1 是星期一
|
||||
- `%U` 年的周数,以星期日为一周的第一天(00..53)
|
||||
- `%V` ISO 周号,以星期一为一周的第一天(01..53)
|
||||
- `%w` 星期(0..6);0 是星期日
|
||||
- `%W` 年的周数,星期一为一周的第一天(00..53)
|
||||
- `%x` 语言环境的日期表示形式(例如,1999年12月31日 / 12/31/99)
|
||||
- `%X` 语言环境的时间表示形式(例如,23:13:48)
|
||||
- `%y` 年的最后两位数字(00..99)
|
||||
- `%Y` 年
|
||||
- `%z` +hhmm 格式的数字时区(例如,-0400)
|
||||
- `%:z` +hh:mm 格式的数字时区(例如,-04:00)
|
||||
- `%::z` +hh:mm:ss 格式的时区(例如 -04:00:00)
|
||||
- `%:::z` 数字时区,带有 `:` 达到必要的精度(例如 -04,+05:30)
|
||||
- `%Z` 字母时区缩写(例如,EDT)
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.networkworld.com/article/3481602/displaying-dates-and-times-your-way-with-linux.html
|
||||
|
||||
作者:[Sandra Henry-Stocker][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[wxy](https://github.com/wxy)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://www.networkworld.com/author/Sandra-Henry_Stocker/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://www.networkworld.com/newsletters/signup.html
|
||||
[2]: https://www.networkworld.com/article/3440100/take-the-intelligent-route-with-consumption-based-storage.html?utm_source=IDG&utm_medium=promotions&utm_campaign=HPE20773&utm_content=sidebar ( Take the Intelligent Route with Consumption-Based Storage)
|
||||
[3]: https://www.facebook.com/NetworkWorld/
|
||||
[4]: https://www.linkedin.com/company/network-world
|
Loading…
Reference in New Issue
Block a user