python时间戳转化成日期数据,日期数据与当前日期加减

import time
import datetime
import numpy as np
#n = len(X_train['create_time'])
#np.zeros(n,dtype = np.int32)
####################单个转换时间戳为时间
def changetime(x):
    timeArray = time.localtime(x)
    otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
    return(otherStyleTime)
####################一列转换成距离当前日期的天数
def nchangetime(x):
    n = len(x)
    now = int(time.time())
    nowtime = changetime(now)
    d1 = datetime.datetime.strptime(nowtime, '%Y-%m-%d %H:%M:%S')
    b = np.zeros(n,dtype=int)
    for i in range(n):
        xx = changetime(x[i])
        d2 = datetime.datetime.strptime(xx, '%Y-%m-%d %H:%M:%S')
        l = (d1-d2).days
        b[i] = l
    return(b)
########转换一列日期数据

X_train['date'].map(changetime)

########转换成距离当前日期的天数

nchangetime(X_train['date'])

上一篇:Python 日期


下一篇:python中datetime的常用操作