Python程序:以不同国家格式显示日期


在Python中,我们有一些内置的函数,例如`datetime.now()`、`utcnow()`、`astimezone()`和`strftime()`,可以用来以不同国家的格式显示时间。在这个程序中,我们将使用`datetime`模块(访问所有与日期和时间相关的内容)和`pytz`模块(允许时区计算和`datetime`的转换)。为了获取不同国家的时区,UTC将由名为`utcnow()`的内置函数设置。问题陈述遵循Python的一些内置函数,例如`datetime.now()`、`utcnow()`、`astimezone()`和`strftime()`,这些函数将用于以不同国家的格式显示时间。

语法

以下语法在示例中使用:

datetime.now()

这是Python中用于查找当前日期和时间的内置时间函数。

utcnow()

这是一个内置函数,返回UTC时区的时间。

astimezone()

这是一个内置函数,返回`datetime`和指定的时区参数。

strftime()

`strftime()`函数是一个内置函数,接受参数来声明时间的格式。

now()

这是Python中的一个内置函数,主要用于查找当前时间和日期。

date.to('Country_time_zone').format('YYYY-MM-DD HH:mm:ss'))

上述语法在示例3中使用:

`date.to('Country_time_zone')`:`date`是将值设置为带有箭头模块的`utcnow()`的变量的名称。`Country_time_zone`设置为国家的时区。例如,印度 - Asia/Kolkata,中国 - Asia/Shanghai等。

`format('YYYY-MM-DD HH:mm:ss')`:`format`主要用于设置国家的日期和时间格式。

示例1

在下面的示例中,我们将首先导入`datetime`和`pytz`模块。然后,我们创建一个名为`get_utc`的变量来使用名为`datetime.utcnow()`的内置函数存储`datetime`的值,该函数将提供世界标准时间。然后,为不同的国家/地区创建变量(例如`get_countries_name`),并使用名为`astimezone()`和`timezone()`的内置函数以及名为`pytz`的模块设置值。接下来,将时间的格式存储在`format_str`变量中。此时间格式作为参数传递给`strftime()`函数,以格式化不同国家/地区的时间。然后,创建不同国家/地区的时间变量,并使用`strftime()`函数通过时间的格式设置值。最后,我们使用不同国家/地区的时间变量打印变量。

import datetime
import pytz
# Using the in-built function datetime.now()
get_utc = datetime.datetime.utcnow()
# Timezone of different countries
get_russia = get_utc.astimezone( pytz.timezone( 'Europe/Moscow' ) )
get_india = get_utc.astimezone( pytz.timezone( 'Asia/Kolkata' ) )
get_china = get_utc.astimezone( pytz.timezone( 'Asia/Shanghai' ) )
get_srilanka = get_utc.astimezone( pytz.timezone( 'Asia/Colombo' ) )
# We will store the format style and passes to the strftime() function
format_str = '%Y-%m-%d %H:%M:%S %Z%z'
time_russia = get_russia.strftime( format_str )
time_india = get_india.strftime( format_str )
time_china = get_china.strftime( format_str )
time_srilanka = get_srilanka.strftime( format_str )
# display the local time of different countries
print( "**************The Time format of different countries**************" )
print( f"Russia: { time_russia }" )
print( f"India: { time_india }" )
print( f"China: { time_china }" )
print( f"Sri Lanka: { time_srilanka }" )

输出

**************The Time format of different countries**************
Russia: 2023-05-29 05:17:14 MSK+0300
India: 2023-05-29 07:47:14 IST+0530
China: 2023-05-29 10:17:14 CST+0800
Sri Lanka: 2023-05-29 07:47:14 +0530+0530

示例2

安装要求

pip install pendulum

将此命令安装到您的系统以运行基于pendulum模块的程序。

将此命令安装到您的系统以运行基于pendulum模块的程序。

在下面的示例中,我们将首先导入名为`pendulum`的模块。然后使用内置方法`now()`,该方法将与`pendulum`变量一起用于在`date`变量中查找当前时间和日期。接下来,我们使用多个`print`函数,将不同国家/地区的时区设置为结果。

import pendulum
date = pendulum.now()
print("The date and time of different countries:\n")
print("New York: ", date.in_timezone('America/New_York').to_datetime_string())
print("London: ", date.in_timezone('Europe/London').to_datetime_string())
print("Paris: ", date.in_timezone('Europe/Paris').to_datetime_string())
print("India: ", date.in_timezone('Asia/Kolkata').to_datetime_string())

输出

The date and time of different countries:

New York:  2023-05-29 04:06:37
London:  2023-05-29 09:06:37
Paris:  2023-05-29 10:06:37
India:  2023-05-29 13:36:37

示例3

安装要求

pip install arrow

将此命令安装到您的系统以运行基于arrow模块的程序。

在下面的示例中,我们首先导入名为`arrow`的模块。然后使用`arrow`模块中的内置方法`utcnow()`,该方法将查找不同国家/地区的日期和时间并将其存储在`date`变量中。接下来,使用`to()`和变量`date`设置日期,这将查找所有不同国家/地区的日期,并进行格式设置以获取时间并使用`print()`函数获取结果。

import arrow
date = arrow.utcnow()
print("*****Different Timezones*****")
print("New York: ", date.to('America/New_York').format('YYYY-MM-DD HH:mm:ss'))
print("India: ", date.to('Asia/Kolkata').format('YYYY-MM-DD HH:mm:ss'))
print("London: ", date.to('Europe/London').format('YYYY-MM-DD HH:mm:ss'))
print("Paris: ", date.to('Europe/Paris').format('YYYY-MM-DD HH:mm:ss'))

输出

*****Different Timezones*****
New York:  2023-05-29 04:15:08
India:  2023-05-29 13:45:08
London:  2023-05-29 09:15:08
Paris:  2023-05-29 10:15:08

结论

上述输出显示了使用`strftime()`、`astimezone()`、`now()`和`timezone()`表示不同国家/地区的时间格式。`utcnow()`函数显示不同国家/地区的世界标准时间。然后我们看到有两个模块,即`pendulum`和`arrow`,它们将显示给定问题陈述的唯一解决方案。

更新于:2023年7月17日

791 次浏览

开启你的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.