热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用Python获取当前日期和时间

使用Python获取当前日期和时间原文:https://ww

使用 Python 获取当前日期和时间

原文:https://www . geesforgeks . org/get-current-date-time-use-python/

先决条件: 日期时间模块

在这个例子中,我们将学习如何使用 Python 获取当前日期和时间。

在 Python 中,日期和时间本身不是一种数据类型,但是可以导入一个名为datetime的模块来处理日期和时间。Datetime 模块内置于 Python 中,无需外部安装。

使用datetime模块的datetime.now()功能获取当前日期和时间。该函数返回当前的本地日期和时间。

语法: datetime.now(tz)

参数:
tz : 需要当前时间和日期的指定时区。(默认使用格林威治子午线时间。)

返回:以时间格式返回当前日期和时间。

例 1:

# Python3 code to demonstrate 
# Getting current date and time using  
# now(). 
# importing datetime module for now() 
import datetime 
# using now() to get current time 
current_time = datetime.datetime.now() 
# Printing value of now. 
print ("Time now at greenwich meridian is : "
                                    , end = "") 
print (current_time) 

输出:

Time now at greenwich meridian is : 2019-12-11 13:54:30.967356

now()的属性:

now()有不同的属性,与年、月、日、时、分、秒等时间属性相同。

例 2:

# Python3 code to demonstrate 
# attributes of now() 
# importing datetime module for now() 
import datetime 
# using now() to get current time 
current_time = datetime.datetime.now() 
# Printing attributes of now(). 
print ("The attributes of now() are : ") 
print ("Year : ", end = "") 
print (current_time.year) 
print ("Month : ", end = "") 
print (current_time.month) 
print ("Day : ", end = "") 
print (current_time.day) 
print ("Hour : ", end = "") 
print (current_time.hour) 
print ("Minute : ", end = "") 
print (current_time.minute) 
print ("Second : ", end = "") 
print (current_time.second) 
print ("Microsecond : ", end = "") 
print (current_time.microsecond) 

输出:

The attributes of now() are :
Year : 2019
Month : 12
Day : 11
Hour : 13
Minute : 56
Second : 7
Microsecond : 292616

获取特定时区的时间:

在上面的例子中,可以看到上面的代码没有给出时区的当前日期和时间。获取特定时区的日期和时间now()将时区作为输入,给出面向时区的输出时间。但是这些时区是在 T2 图书馆定义的。

例:3

# Python3 code to demonstrate 
# attributes of now() for timezone 
# for now() 
import datetime 
# for timezone() 
import pytz 
# using now() to get current time 
current_time = datetime.datetime.now(pytz.timezone('Asia/Kolkata')) 
# printing current time in india 
print ("The current time in india is : ") 
print (current_time)  

输出:

The current time in india is :
2019-12-11 19:28:23.973616+05:30

推荐阅读
  • 本文介绍了在处理不规则数据时如何使用Python自动提取文本中的时间日期,包括使用dateutil.parser模块统一日期字符串格式和使用datefinder模块提取日期。同时,还介绍了一段使用正则表达式的代码,可以支持中文日期和一些特殊的时间识别,例如'2012年12月12日'、'3小时前'、'在2012/12/13哈哈'等。 ... [详细]
  • 原文链接:Python:获取“3年前的今天”的日期时间Python:getdatetimefor3yearsagotoday在Python中,如何获取3年前的今天的datetime ... [详细]
  • 本文介绍了在Python3中如何使用选择文件对话框的格式打开和保存图片的方法。通过使用tkinter库中的filedialog模块的asksaveasfilename和askopenfilename函数,可以方便地选择要打开或保存的图片文件,并进行相关操作。具体的代码示例和操作步骤也被提供。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 使用Ubuntu中的Python获取浏览器历史记录原文: ... [详细]
  • 树莓派语音控制的配置方法和步骤
    本文介绍了在树莓派上实现语音控制的配置方法和步骤。首先感谢博主Eoman的帮助,文章参考了他的内容。树莓派的配置需要通过sudo raspi-config进行,然后使用Eoman的控制方法,即安装wiringPi库并编写控制引脚的脚本。具体的安装步骤和脚本编写方法在文章中详细介绍。 ... [详细]
  • 我正在编写一个脚本,它将根据以下内容从基本HTML页面中提取数据:URL中的第一个参数在-90.0和90.0(含)之间浮动,第二个数字在- ... [详细]
  • 如何在Python中对时间进行转换
    这篇文章主要介绍了如何在Python中对时间进行转换,亿速云小编觉得不错,现在分享给大家,也给大家做个参考,一起跟随亿速云小编来看看吧!Pytho ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 本文介绍了计算机网络的定义和通信流程,包括客户端编译文件、二进制转换、三层路由设备等。同时,还介绍了计算机网络中常用的关键词,如MAC地址和IP地址。 ... [详细]
  • importjava.util.ArrayList;publicclassPageIndex{privateintpageSize;每页要显示的行privateintpageNum ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • WindowsAPI的时间结构体、时间转换及时间获取 ... [详细]
author-avatar
路人不会编程
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有