Python中时间的处理

我碰到的问题:

1 取出的时间为字符串,需要和当前时间比较判断是否大于或者小于一个给定的值

1:将字符串转换为int:

int(keepday)


2 获取当前系统时间:

  B=datetime.datetime.now()

3 时间差variance=datetime.timedelta(days=int(keepday))


完整的函数如下:

    def timeCompare(self,instancecreateTime,keepday):
        variance=datetime.timedelta(days=int(keepday))
        #print strftime("%Y-%m-%d", date)
        print instancecreateTime+variance
        B=datetime.datetime.now()
        return B-instancecreateTime >variance
      

问题补充:

import datetime
import time

format="%Y-%m-%d %H:%M:%S"
t1=time.strptime("2008-01-31 00:11:23",format)
t2=datetime.datetime(t1[0],t1[1],t1[2],t1[3],t1[4],t1[5],t1[6])
t3=t2-datetime.timedelta(minutes=30)
t3=str(t3)

b1=t3[0:4]
b2=t3[5:7]
b3=t3[8:10]
b4=t3[11:13]
b5=t3[14:16]
b6=t3[-2:]

print b1
print b2
print b3
print b4
print b5
print b6

output  as follows:
2008
01
30
23
41
03
计算两个日期相差天数的计算。

d1 = datetime.datetime(2008, 2, 16)
d2 = datetime.datetime(2009, 12, 31)

# 结果:47
print (d1 - d2).days

localtime和gtime的区别:


import time
now = time.time()
time.localtime()
(2007, 6, 2, 12, 47, 7, 5, 153, 0)
localtime返回tuple格式的时间
gmtime()返回的是0时区的值,
localtime返回的是当前时区的值。

#查看时区用
>>> time.timezone
-28800
上面的值是一个秒值,是当前时区和0时区相差的描述,-28800=-8*3600,即为东八区。



time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime( time.time() ) )

再把格式记录如下

%a 星期几的简写 Weekday name, abbr.
%A 星期几的全称 Weekday name, full
%b 月分的简写 Month name, abbr.
%B 月份的全称 Month name, full
%c 标准的日期的时间串 Complete date andtime representation
%d 十进制表示的每月的第几天 Day of the month
%H 24小时制的小时 Hour (24-hour clock)
%I 12小时制的小时 Hour (12-hour clock)
%j 十进制表示的每年的第几天 Day of the year
%m 十进制表示的月份 Month number
%M 十时制表示的分钟数 Minute number
%S 十进制的秒数 Second number
%U 第年的第几周,把星期日做为第一天(值从0到53)Week number (Sunday first weekday)
%w 十进制表示的星期几(值从0到6,星期天为0)weekday number
%W 每年的第几周,把星期一做为第一天(值从0到53) Week number (Monday first weekday)
%x 标准的日期串 Complete date representation (e.g. 13/01/08)
%X 标准的时间串 Complete time representation (e.g. 17:02:10)
%y 不带世纪的十进制年份(值从0到99)Year number within century
%Y 带世纪部分的十制年份 Year number
%z,%Z 时区名称,如果不能得到时区名称则返回空字符。Name of time zone
%% 百分号


计算当前日期之后多少天的时间

        d1 = datetime.datetime.now()
        d3 = d1 + datetime.timedelta(days =10)

        print str(d3)
        print d3.ctime()

 #-*-coding:utf-8-*-  
import datetime, calendar,time

def getYesterday():   #
   today=datetime.date.today()  
   oneday=datetime.timedelta(days=1)  
   yesterday=today-oneday   
   return yesterday
 
     
def getToday():   #2010-06-04
    return datetime.date.today()
 
#获取给定参数的前几天的日期,返回一个list  
def getDaysByNum(num):   #5,['2010-06-03', '2010-06-02', '2010-06-01', '2010-05-31', '2010-05-30']
    today=datetime.date.today()  
    oneday=datetime.timedelta(days=1)      
    li=[]       
    for i in range(0,num):  
        #今天减一天,一天一天减  
        today=today-oneday  
        #把日期转换成字符串  
        #result=datetostr(today)  
        li.append(datetostr(today))  
    return li  
 
#将字符串转换成datetime类型  
def strtodatetime(datestr,format):      
    return datetime.datetime.strptime(datestr,format)  
 
#时间转换成字符串,格式为2008-08-02  
def datetostr(date):    
    return   str(date)[0:10]  
 
#两个日期相隔多少天,例:2008-10-03和2008-10-01是相隔两天  
def datediff(beginDate,endDate):  
    format="%Y-%m-%d";  
    bd=strtodatetime(beginDate,format)  
    ed=strtodatetime(endDate,format)      
    oneday=datetime.timedelta(days=1)  
    count=0
    while bd!=ed:  
        ed=ed-oneday  
        count+=1
    return count  
 
#获取两个时间段的所有时间,返回list  
def getDays(beginDate,endDate):  
    format="%Y-%m-%d";  
    bd=strtodatetime(beginDate,format)  
    ed=strtodatetime(endDate,format)  
    oneday=datetime.timedelta(days=1)   
    num=datediff(beginDate,endDate)+1   
    li=[]  
    for i in range(0,num):   
        li.append(datetostr(ed))  
        ed=ed-oneday  
    return li  
 
#获取当前的年、月、日、时、分、秒,返回字符串。
def getNow():   #2010-06-04 11:46:04.992000
    return datetime.datetime.now()
   
def getYear(year):
   if year == 2:
      return time.strftime("%y",time.localtime()) #2位数年
   if year == 4:
      return time.strftime("%Y",time.localtime())   #4位数年
    
def getMonth(month):
   if month == "num":
      return time.strftime("%m",time.localtime()) #数字月份01-12
   if month == "s":
      return time.strftime("%b",time.localtime()) #月份简写
   if month == "all":
      return time.strftime("%B",time.localtime()) #月份全写
   
def getDay():   #数字01-31
    return time.strftime("%d",time.localtime())

def getdayforyear(): #一年中的第几天
   return time.strftime("%j",time.localtime())

def gethours(hour):
   if hour == "H":  #当前小时,24小时制
      return time.strftime("%H",time.localtime())
   if hour == "I":  #当前小时,12小时制
      return time.strftime("%I %p",time.localtime())
 
def getminute(): #当前时间分钟
   return time.strftime("%M",time.localtime())
 
def getsecond(): #当前时间秒
   return time.strftime("%S",time.localtime())

def chinatime():
   return str(getToday())+ " "+time.strftime("%X",time.localtime())
 
def engtime():
   return time.strftime("%c",time.localtime())


def getweekly(weekly):
   if weekly == "U": #一年中的第几周,且是星期天作为周的第一天
      return time.strftime("%U",time.localtime())
   if weekly == "W": #一年中的第几周,且是星期一作为周的第一天
      return time.strftime("%W",time.localtime())
     
     
 
print getminute()

'''     
print getToday()  
print getYesterday()  
print getDaysByNum(3)  
print getDays('2008-10-01','2008-10-05')  
print '2008-10-04 00:00:00'[0:10]  
 
print str(getYear())+getMonth()+getDay()+"-"  
print getNow()
'''


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

惹不起的程咬金

来都来了,不赏点银子么

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值