使用time:
三种表达方式分别为:自定义的格式型字符串时间(format_time)(自定义的、非自定义) ——>其中自定义的可由struct_time通过strftime(format,struct_time)转换,非自定义的可由struct_time通过asctime()转换,stamp_time通过ctime()转换
元组型时间(struct_time) -------->可由formattime通过time_strp()转换,stamp_time通过localtime()、gmtime转换
时间戳(time_stamp) 通过struct_time()通过mktime转换
时间戳 #time_stamp时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。
使用datetime:
实例:
In:
import pandas as pd
import time
import datetime
a = pd.datetime.now().year
print("a is :", a)
b = pd.Timestamp('2012-01-01 00:00:08')
print("type(b) is :",type(b))
a1 = time.time()
a = datetime.datetime.fromtimestamp(time.time())
print("a1, a:",(a1, a))
b = datetime.datetime.utcfromtimestamp(time.time())
print("a1, b:", (a1, b))
c = a.timestamp()
print("c is :", c)
d = datetime.datetime.timestamp(a)
print(" d is :", d)
e = datetime.datetime.strftime(a,"%Y-%m-%d %H:%M:%S.%f")
print("e is :", e)
f = datetime.datetime.isoformat(a,sep="#")
print("f is :", f)
g = datetime.datetime.strptime("2017-07-07 09:09:09.66666","%Y-%m-%d %H:%M:%S.%f")
print("g is :", g)
h = datetime.datetime.strptime("2017/08/02;09:29:36","%Y/%m/%d;%H:%M:%S")
print("h is :", h)
i = datetime.datetime.timestamp(h)
print("i is ::", i)
j = datetime.datetime.fromtimestamp(float(i))
print("j is :", j)
k = datetime.datetime.strftime(j,"%Y--%m--%d -%H::%M::%S")
print("k is :", k)
k1 = datetime.datetime.strftime(datetime.datetime.fromtimestamp(time.time()),"%Y--%m--%d -%H::%M::%S")
print("k1 is :", k1)
k2 =datetime.datetime.now().strftime("%Y--%m--%d -%H::%M::%S")
print("k2 is :", k2)
k3 = datetime.datetime.now().timestamp()
print("k3 is :", k3)
dt = datetime.datetime.now()
out:
a is : 2018
type(b) is :
a1, a: (1533177058.961467, datetime.datetime(2018, 8, 2, 10, 30, 58, 961503))
a1, b: (1533177058.961467, datetime.datetime(2018, 8, 2, 2, 30, 58, 961631))
c is : 1533177058.961503
d is : 1533177058.961503
e is : 2018-08-02 10:30:58.961503
f is : 2018-08-02#10:30:58.961503
g is : 2017-07-07 09:09:09.666660
h is : 2017-08-02 09:29:36
i is :: 1501637376.0
j is : 2017-08-02 09:29:36
k is : 2017--08--02 -09::29::36
k1 is : 2018--08--02 -10::30::58
k2 is : 2018--08--02 -10::30::58
k3 is : 1533177058.962937
此图引用自:https://blog.csdn.net/qy6kwyec/article/details/54980969