Python 以不同的国家格式显示日期的程序
在Python中,我们有一些内置的时间函数,如datetime.now()、utcnow()、astimezone()和strftime(),可以用来以不同的国家格式显示时间。在这个程序中,我们将使用以下模块:
datetime (用于访问与日期和时间相关的所有内容)和 pytz (允许进行时区计算和datetime的转换)。要获取不同国家的时区,将使用名为utcnow()的内置函数来设置UTC。问题陈述使用了Python的一些内置函数,如datetime.now()、utcnow()、astimezone()和strftime(),这些函数将用于以不同的国家格式显示时间。
语法
示例中使用了以下语法:
datetime.now()
这是Python中的一个内置时间函数,用于查找当前日期和时间。
utcnow()
这是一个内置函数,返回的是UTC时区的时间。
astimezone()
这是一个内置函数,它返回指定时区参数的日期时间。
strftime()
strftime()函数是一个内置函数,它接受参数来声明时间的格式。
now()
这是Python中的内置函数,主要用于获取当前的时间和日期。
date.to('Country_time_zone').format('YYYY-MM-DD HH:mm:ss'))
上述语法用于示例3−
date.to(‘Country_time_zone’):日期是设置为utcnow()的变量名称,使用了箭头模块。Country_time_zone被设置为所在国家的时区。例如:印度-Asia/Kolkata,中国-Asia/Shanghai等。
format(‘YYYY-MM-DD HH:mm:ss’):format主要用于设置国家的日期和时间格式。
示例1
在下面的示例中,我们首先导入了datetime和pytz两个模块。然后我们创建了一个名为get_utc的变量,该变量存储了使用datetime.utcnow()内置函数获取到的全球标准时间。接下来,我们为不同国家创建了变量(例如get_countries_name),并使用pytz模块的astimezone()和timezone()内置函数设置了值。然后,我们将时间格式存储在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的模块启动程序。然后使用内置的now()方法,该方法将用于在变量date中使用pendulum变量找到当前时间和日期。接下来,我们使用了多重打印函数,将各个国家的不同时区设置为结果。
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的模块开始程序。然后使用带有箭头模块的内置方法utcnow()来查找不同国家的日期和时间,并将其存储在变量date中。接下来,使用to()方法设置日期,该方法将查找所有不同国家的日期,并通过format方法设置时间,并使用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,它们将展示给定问题陈述的独特解决方案。