如何获取当前时间?


当前回答

时间模块可以导入各种时间信息,包括睡眠和其他类型的信息,包括当前时间类型

import time
time.strftime("%T", time.localtime())

输出应如下所示

05:46:33
11:22:56
13:44:55
22:33:44
00:00:00

其他回答

方法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

这很简单。尝试:

import datetime
date_time = str(datetime.datetime.now())
date = date_time.split()[0]
time = date_time.split()[1]

now()的属性可用于获取python中的当前时间:

# 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)

如果您只希望当前时间戳以毫秒为单位(例如,测量执行时间),也可以使用“timeit”模块:

import timeit
start_time = timeit.default_timer()
do_stuff_you_want_to_measure()
end_time = timeit.default_timer()
print("Elapsed time: {}".format(end_time - start_time))

您可以使用时间模块:

>>> import time
>>> print(time.strftime("%d/%m/%Y"))
06/02/2015

使用大写Y表示全年,使用Y表示2015年2月6日。

您也可以使用以下代码来延长时间:

>>> time.strftime("%a, %d %b %Y %H:%M:%S")
'Fri, 06 Feb 2015 17:45:09'