如何从命令行在Linux和Windows中打印当前日期和时间?

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.

对于不同的系统和编程语言,获取当前日期时间信息非常不同。 我们将研究绝大多数操作系统和编程语言的一个简单但重要的问题。

Linux / FreeBSD / Unix (Linux/FreeBSD/Unix)

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
Linux FreeBSD Unix date and time with Different Formats
Linux FreeBSD Unix date and time with Different Formats
不同格式Linux FreeBSD Unix日期和时间

只有小时和分钟(Only Hour and Minute)

We can provide parameters to get only time partition from date command.

我们可以提供参数以仅从date命令获取时间分区。

$ date +%H:%M

年月日格式 (Year Month Day Format)

We can get current year month day like below

我们可以得到如下所示的当前年度月份

$ date +%Y_%m_%d

年月日格式 (Year-Month-Day Format)

We can use ready to use parameters like +%F which will list year-month-day

我们可以使用现成可用的参数,例如+%F ,它将列出年月日

$ date +%F

月/日/年格式 (Month/Day/Year Format)

We can use +%D to get a date according to locale settings

我们可以使用+%D根据地区设置获取日期

$ date +%D

在Windows命令行中显示日期和时间 (Display Date and Time In Windows Command Line)

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操作系统提供datetime命令,以便打印和设置当前日期和时间。 发出命令日期和时间后,将显示当前日期和时间,然后将要求我们提供新的日期和时间。 我们可以通过按Enter键跳过此操作,或者根本不输入任何值。

> date

> time
Display Date and Time In Windows Command Line
Display Date and Time In Windows Command Line
在Windows命令行中显示日期和时间

Python日期时间模块和函数(Python Datetime Module and Functions)

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")
Python Datetime Module and Functions
Python Datetime Module and Functions
Python日期时间模块和函数

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")
Python Datetime Module and Functions
Python Datetime Module and Functions
Python日期时间模块和函数
LEARN MORE  Linux Time Command Tutorial With Examples To Get Programs Resource Usage
了解更多Linux时间命令教程,并附带示例来获取程序资源的使用情况

翻译自: https://www.poftut.com/how-to-print-current-date-and-time-in-linux-and-windows-from-command-line/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值