如何获取当前时间?
当前回答
默认情况下,now()函数以YYYY-MM-DD HH:MM:SS:MS格式返回输出。使用以下示例脚本获取Python脚本中的当前日期和时间,并在屏幕上打印结果。使用以下内容创建文件getDateTime1.py。
import datetime
currentDT = datetime.datetime.now()
print (str(currentDT))
输出如下所示:
2018-03-01 17:03:46.759624
其他回答
以下是我用来获取时间而不必格式化的内容。有些人不喜欢拆分方法,但它在这里很有用:
from time import ctime
print ctime().split()[3]
它将以HH:MM:SS格式打印。
方法1:从系统datetime获取当前日期和时间
datetime模块提供用于操作日期和时间的类。
密码
from datetime import datetime,date
print("Date: "+str(date.today().year)+"-"+str(date.today().month)+"-"+str(date.today().day))
print("Year: "+str(date.today().year))
print("Month: "+str(date.today().month))
print("Day: "+str(date.today().day)+"\n")
print("Time: "+str(datetime.today().hour)+":"+str(datetime.today().minute)+":"+str(datetime.today().second))
print("Hour: "+str(datetime.today().hour))
print("Minute: "+str(datetime.today().minute))
print("Second: "+str(datetime.today().second))
print("MilliSecond: "+str(datetime.today().microsecond))
输出将如下
Date: 2020-4-18
Year: 2020
Month: 4
Day: 18
Time: 19:30:5
Hour: 19
Minute: 30
Second: 5
MilliSecond: 836071
方法2:如果网络可用,则获取当前日期和时间
urllib包帮助我们处理url,即网页。这里我们从网页收集数据http://just-the-time.appspot.com/并使用包日期解析器从网页解析日期时间。
密码
from urllib.request import urlopen
import dateparser
time_url = urlopen(u'http://just-the-time.appspot.com/')
datetime = time_url.read().decode("utf-8", errors="ignore").split(' ')[:-1]
date = datetime[0]
time = datetime[1]
print("Date: "+str(date))
print("Year: "+str(date.split('-')[0]))
print("Month: "+str(date.split('-')[1]))
print("Day: "+str(date.split('-')[2])+'\n')
print("Time: "+str(time))
print("Hour: "+str(time.split(':')[0]))
print("Minute: "+str(time.split(':')[1]))
print("Second: "+str(time.split(':')[2]))
输出将如下
Date: 2020-04-18
Year: 2020
Month: 04
Day: 18
Time: 14:17:10
Hour: 14
Minute: 17
Second: 10
方法3:从机器的本地时间获取当前日期和时间
Python的时间模块提供了一个函数,用于从称为localtime()的历元起经过的秒数获取本地时间。ctime()函数将从epoch开始经过的秒数作为参数,并返回表示本地时间的字符串。
密码
from time import time, ctime
datetime = ctime(time()).split(' ')
print("Date: "+str(datetime[4])+"-"+str(datetime[1])+"-"+str(datetime[2]))
print("Year: "+str(datetime[4]))
print("Month: "+str(datetime[1]))
print("Day: "+str(datetime[2]))
print("Week Day: "+str(datetime[0])+'\n')
print("Time: "+str(datetime[3]))
print("Hour: "+str(datetime[3]).split(':')[0])
print("Minute: "+str(datetime[3]).split(':')[1])
print("Second: "+str(datetime[3]).split(':')[2])
输出将如下
Date: 2020-Apr-18
Year: 2020
Month: Apr
Day: 18
Week Day: Sat
Time: 19:30:20
Hour: 19
Minute: 30
Second: 20
获取当前时间并将其转换为字符串:
from datetime import datetime
datetime.now().strftime('%Y-%m-%d %H:%M:%S')
您可以尝试以下操作
import datetime
now = datetime.datetime.now()
print(now)
or
import datetime
now = datetime.datetime.now()
print(now.strftime("%Y-%b-%d, %A %I:%M:%S"))
因为还没有人提到它,这是我最近遇到的事情。。。pytz时区的fromutc()方法与datetime的utcnow()相结合是我发现的在任何时区中获取有用的当前时间(和日期)的最佳方法。
from datetime import datetime
import pytz
JST = pytz.timezone("Asia/Tokyo")
local_time = JST.fromutc(datetime.utcnow())
如果您只需要时间,那么可以使用local_time.time()获取时间。
推荐文章
- Numpy Max vs amax vs maximum
- 我应该在.gitignore文件中添加Django迁移文件吗?
- 每n行有熊猫
- 实例属性attribute_name定义在__init__之外
- 如何获取在Python中捕获的异常的名称?
- 第一次出现的值大于现有值的Numpy
- 如何从Python函数中返回两个值?
- 前一个月的Python日期
- Python中方括号括起来的列表和圆括号括起来的列表有什么区别?
- Python日志记录不输出任何东西
- 每n秒运行特定代码
- SQLAlchemy是否有与Django的get_or_create等价的函数?
- 如何将python datetime转换为字符串,具有可读格式的日期?
- 美丽的汤和提取div及其内容的ID
- 在Python中重置生成器对象