赞
踩
Getting current date-time information is very different for different systems and programming languages. We will look at this simple but big issue for the vast majority of operating systems and programming languages.
对于不同的系统和编程语言,获取当前日期时间信息非常不同。 我们将研究绝大多数操作系统和编程语言的一个简单但重要的问题。
We can date information from the GUI panel clock but we will look for the command line. We will issue date command to get the current date and time. Linux date format has a lot of options. Alternatively, we can call these as Unix date or bash date examples.
我们可以从GUI面板时钟中确定日期,但是我们会寻找命令行。 我们将发出date命令以获取当前日期和时间。 Linux日期格式有很多选择。 另外,我们可以将它们称为Unix日期或bash日期示例。
$ date
We can provide parameters to get only time partition from date command.
我们可以提供参数以仅从date命令获取时间分区。
$ date +%H:%M
We can get current year month day like below
我们可以得到如下所示的当前年度月份
$ date +%Y_%m_%d
We can use ready to use parameters like +%F which will list year-month-day
我们可以使用现成可用的参数,例如+%F ,它将列出年月日
$ date +%F
We can use +%D to get a date according to locale settings
我们可以使用+%D根据地区设置获取日期
$ date +%D
Windows operating system provides date
and time
commands in order to print and set current date and time. After issuing the command date and time the current date and time will be displayed and then we will be asked for a new date and time. We can skip this just by pressing Enter or simply do not entering any value.
Windows操作系统提供date
和time
命令,以便打印和设置当前日期和时间。 发出命令日期和时间后,将显示当前日期和时间,然后将要求我们提供新的日期和时间。 我们可以通过按Enter键跳过此操作,或者根本不输入任何值。
- > date
-
- > time
Python is a scripting language and to get current date time information python function in a code file can be used. An alternative is python interpreter. In this example, we will use an interpreter.
Python是一种脚本语言,可以使用代码文件中的python函数来获取当前日期时间信息。 替代方法是python解释器。 在此示例中,我们将使用解释器。
- from datetime import datetime
- now=datetime.now()
- now.strftime("%A, %d. %B %Y %I:%M%p")
We can get time information with %I:%M format string parameters.
我们可以使用%I:%M格式字符串参数获取时间信息。
now.strftime("%I:%M")
We get the year, month and day as a string like below
我们将年,月和日作为字符串,如下所示
now.strftime("%Y-%m-%d")
翻译自: https://www.poftut.com/how-to-print-current-date-and-time-in-linux-and-windows-from-command-line/
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。