import time, datetime
print(datetime.datetime.now().time()) # 11:20:08.272239
# Or in a more complicated wayprint(datetime.datetime.now().time().isoformat()) # 11:20:08.272239print(datetime.datetime.now().time().strftime('%H:%M:%S.%f')) # 11:20:08.272239
# But do not use this:print(time.strftime("%H:%M:%S.%f", time.localtime()), str) # 11:20:08.%f
import pandas as pdprint(pd.datetime.now())print(pd.datetime.now().date())print(pd.datetime.now().year)print(pd.datetime.now().month)print(pd.datetime.now().day)print(pd.datetime.now().hour)print(pd.datetime.now().minute)print(pd.datetime.now().second)print(pd.datetime.now().microsecond)
from time import ctime
// Day {Mon,Tue,..}print ctime().split()[0]// Month {Jan, Feb,..}print ctime().split()[1]// Date {1,2,..}print ctime().split()[2]// HH:MM:SSprint ctime().split()[3]// Year {2018,..}print ctime().split()[4]
当您调用ctime()时,它会将秒转换为格式'Day Month Date HH:MM:SS Year'的字符串(例如:'Wed January 17 16:53:22 2018'),然后您调用split()方法,该方法将从您的字符串['Wed','Jan','17','16:56:45','2018'](默认分隔符是空格)中创建一个列表。
import datetime
currentDT = datetime.datetime.now()
print ("Current Year is: %d" % currentDT.year)print ("Current Month is: %d" % currentDT.month)print ("Current Day is: %d" % currentDT.day)print ("Current Hour is: %d" % currentDT.hour)print ("Current Minute is: %d" % currentDT.minute)print ("Current Second is: %d" % currentDT.second)print ("Current Microsecond is: %d" % currentDT.microsecond)
#!/usr/bin/pythonimport time;
ticks = time.time()print "Number of ticks since "12:00am, Jan 1, 1970":", ticks
>>> from datetime import datetime>>> now = datetime.now() #get a datetime object containing current date and time>>> current_time = now.strftime("%H:%M:%S") #created a string representing current time>>> print("Current Time =", current_time)Current Time = 17:56:54
import pytzfrom datetime import datetime
#UTC Timeprint("UTC Date and time")epoch: datetime =datetime.now().replace(tzinfo=pytz.utc)print(epoch)
#local date and timeprint("Local Date and time")today = datetime.now()local_time = today.strftime("%Y-%M-%d:%H:%M:%S")print(local_time)
#convert time to AM PM formatprint("Date and time AM and PM")now = today.strftime("%Y-%M-%d:%I:%M %p")print(now)
from django.utils import timezone
timezone.now() // for date time
timezone.now().year // for yaer
timezone.now().month // for month
timezone.now().day // for day
timezone.now().date // for date
timezone.now().hour // for hour
timezone.now().weekday // for minute
或者如果你想在python上使用
import time
time.strftime('%X') // '13:12:47'
time.strftime('%x') // '01/20/22'
time.strftime('%d') // '20' day
time.strftime('%m') // '01' month
time.strftime('%y') // '20' year
time.strftime('%H') // '01' hour
time.strftime('%M') // '01' minute
time.strftime('%m') // '01' second