如何获取当前时间?
当前回答
使用日期时间:
>>> import datetime
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2009, 1, 6, 15, 8, 24, 78915)
>>> print(now)
2009-01-06 15:08:24.789150
对于没有日期的时钟时间:
>>> now.time()
datetime.time(15, 8, 24, 78915)
>>> print(now.time())
15:08:24.789150
要保存键入,可以从datetime模块导入datetime对象:
>>> from datetime import datetime
然后删除前缀datetime。从以上所有方面来看。
其他回答
如果你经常使用panda,你可以使用Timestamp,它相当于Python的Datetime:
In [1]: import pandas as pd
In [2]: pd.Timestamp.now()
Out[2]: Timestamp('2022-06-21 21:52:50.568788')
只是时间:
In [3]: pd.Timestamp.now().strftime("%H:%M:%S")
Out[3]: '21:53:01'
使用日期时间:
>>> import datetime
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2009, 1, 6, 15, 8, 24, 78915)
>>> print(now)
2009-01-06 15:08:24.789150
对于没有日期的时钟时间:
>>> now.time()
datetime.time(15, 8, 24, 78915)
>>> print(now.time())
15:08:24.789150
要保存键入,可以从datetime模块导入datetime对象:
>>> from datetime import datetime
然后删除前缀datetime。从以上所有方面来看。
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:SS
print 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'中列出一个列表(默认delimeter为空格)。
括号用于在列表中“选择”所需参数。
应该只调用一个代码行。人们不应该像我那样称呼它们,这只是一个例子,因为在某些情况下,你会得到不同的值,这是罕见但并非不可能的情况。
获取当前日期时间属性:
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/python
import time;
ticks = time.time()
print "Number of ticks since "12:00am, Jan 1, 1970":", ticks
这就是我最终要做的:
>>>from time import strftime
>>>strftime("%m/%d/%Y %H:%M")
01/09/2015 13:11
此外,该表是选择适当格式代码以按照您想要的方式格式化日期的必要参考(来自Python“datetime”文档)。
推荐文章
- 如何排序mongodb与pymongo
- 不可变与可变类型
- 列表是线程安全的吗?
- 在Android应用程序中显示当前时间和日期
- 字符串不能识别为有效的日期时间“格式dd/MM/yyyy”
- 操作系统。makdirs在我的路径上不理解“~”
- 如何在Django模板中获得我的网站的域名?
- 如何转换日期时间?将日期时间
- 在django Forms中定义css类
- 如何在Python中scp ?
- Numpy Max vs amax vs maximum
- 我应该在.gitignore文件中添加Django迁移文件吗?
- 每n行有熊猫
- 实例属性attribute_name定义在__init__之外
- 如何获取在Python中捕获的异常的名称?