首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

python实现时间的比较

下面介绍下Python中比较两个日期大小的脚本. 在实际工作中用到,以供后续参考及备忘 时间区间比较函数 import time # 这里比较l_time 是否在时间区间[start_t, end_t]中 def compare_time(l_time,start_t,end_t):     s_time = time.mktime(time.strptime(start_t,'%Y%m%d%H%M')) # get the seconds for specify date     e_time = time.mktime(time.strptime(end_t,'%Y%m%d%H%M'))     log_time = time.mktime(time.strptime(l_time,'%Y-%m-%d %H:%M:%S'))     if (float(log_time) >= float(s_time)) and (float(log_time) <= float(e_time)):         return True     return False # 在用mktime时,注意import time 模块,否则会提示找不到改attribute.   输入的时间格式必须跟自己的格式化串保持一致. 如时间: "2011-11-10 14:56:58"  定义格式串时应该为: "%Y-%m-%d %H:%M:%S" 有试过 from datetim import datetime,time , 但是没有成功,暂时没有考虑去解决.

01
  • 您找到你想要的搜索结果了吗?
    是的
    没有找到

    Python 学习入门(10)—— 时间

    Python格式化日期时间的函数为datetime.datetime.strftime();由字符串转为日期型的函数为:datetime.datetime.strptime(),两个函数都涉及日期时间的格式化字符串,列举如下: %a     Abbreviated weekday name %A     Full weekday name %b     Abbreviated month name %B     Full month name %c     Date and time representation appropriate for locale %d     Day of month as decimal number (01 - 31) %H     Hour in 24-hour format (00 - 23) %I     Hour in 12-hour format (01 - 12) %j     Day of year as decimal number (001 - 366) %m     Month as decimal number (01 - 12) %M     Minute as decimal number (00 - 59) %p     Current locale's A.M./P.M. indicator for 12-hour clock %S     Second as decimal number (00 - 59) %U     Week of year as decimal number, with Sunday as first day of week (00 - 51) %w     Weekday as decimal number (0 - 6; Sunday is 0) %W     Week of year as decimal number, with Monday as first day of week (00 - 51) %x     Date representation for current locale %X     Time representation for current locale %y     Year without century, as decimal number (00 - 99) %Y     Year with century, as decimal number %z, %Z     Time-zone name or abbreviation; no characters if time zone is unknown %%     Percent sign

    03

    扫码

    添加站长 进交流群

    领取专属 10元无门槛券

    手把手带您无忧上云

    扫码加入开发者社群

    热门标签

    活动推荐

      运营活动

      活动名称
      广告关闭
      领券