在Python中,通常有这几种方式来表示时间:

  • 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。
  • 格式化的时间字符串(Format String)
  • 结构化的时间(struct_time):struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天,夏令时)
    import time
    print(time.time()) # 时间戳:1487130156.419527
    print(time.strftime("%Y-%m-%d %X")) #格式化的时间字符串:'2017-02-15 11:40:53'
    print(time.localtime()) #结构化的时间:本地时区的struct_time
    print(time.gmtime()) #UTC时区的struct_time

    # localtime([secs])
    # 将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。
    time.localtime()
    time.localtime(1473525444.037215)
    # gmtime([secs]) 和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。
    # mktime(t) : 将一个struct_time转化为时间戳。
    print(time.mktime(time.localtime()))#1473525749.0 # strftime(format[, t]) : 把一个代表时间的元组或者struct_time(如由time.localtime()和
    # time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。如果元组中任何一个
    # 元素越界,ValueError的错误将会被抛出。
    print(time.strftime("%Y-%m-%d %X", time.localtime()))#2016-09-11 00:49:56 # time.strptime(string[, format])
    # 把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。
    print(time.strptime('2011-05-05 16:37:06', '%Y-%m-%d %X'))
    #time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=16, tm_min=37, tm_sec=6,
    # tm_wday=3, tm_yday=125, tm_isdst=-1)
    #在这个函数中,format默认为:"%a %b %d %H:%M:%S %Y"。 # ctime([secs]) : 把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为
    # None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。
    print(time.ctime()) # Sun Sep 11 00:46:38 2016
    print(time.ctime(time.time())) # Sun Sep 11 00:46:38 2016

    # asctime([t]) : 把一个表示时间的元组或者struct_time表示为这种形式:'Sun Jun 20 23:21:05 1993'。
    # 如果没有参数,将会将time.localtime()作为参数传入。
    print(time.asctime())#Sun Sep 11 00:43:43 2016 # ctime([secs]) : 把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为
    # None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。
    print(time.ctime()) # Sun Sep 11 00:46:38 2016
    print(time.ctime(time.time())) # Sun Sep 11 00:46:38 2016
    # datetime 模块
    
    #时间加减 周/日/时/分/毫秒
    import datetime print(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925
    print(datetime.date.fromtimestamp(time.time()) ) # 时间戳直接转成日期格式 2016-08-19
    print(datetime.datetime.now() )
    print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天
    print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天
    print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时
    print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分 c_time = datetime.datetime.now()
    print(c_time.replace(minute=3,hour=2)) #时间替换
# 时间加减,月/日
import calendar
import math def get_recent_month(dt, months):
# 这里的months 参数传入的是正数表示往后推,负数表示往前推
month = dt.month - 1 + months
year = dt.year + math.floor(month / 12)
month = month % 12 + 1
day = min(dt.day, calendar.monthrange(year, month)[1])
recent_date = datetime.date(year, month, day).strftime('%Y%m%d')
return recent_date def get_recent_day(dt, days):
# days 负数是向前推,正数是向后推
# datetime.timedelta 两个时间之间的时间差
delta = datetime.timedelta(days=days)
# 获取delta前的时间
beginPushDate = (dt + delta).strftime('%Y%m%d')
return beginPushDate dt = datetime.datetime.strptime(str(''), '%Y%m%d')
# 当前日期前推3个月
print(get_recent_month(dt, -3))
# 当前日期后推3个月
print(get_recent_month(dt, 3))
# 当前日期前推3天
print(get_recent_day(dt, -3))
# 当前日期后推3天
print(get_recent_day(dt, 3))

time 和 datetime 模块的更多相关文章

  1. python中datetime模块

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致.相比于time模块 ...

  2. python datetime模块参数详解

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块,它提供 的接口与C标准库time.h基本一致.相比于time模块,datetime模块的接 ...

  3. Python处理时间 time && datetime 模块

    Python处理时间 time  &&  datetime 模块 个人整理,获取时间方式: import datetime import time #获取当前时间:Thu Nov 03 ...

  4. python time模块和datetime模块详解

    一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...

  5. python-Day5-深入正则表达式--冒泡排序-时间复杂度 --常用模块学习:自定义模块--random模块:随机验证码--time & datetime模块

    正则表达式   语法:             mport re #导入模块名 p = re.compile("^[0-9]") #生成要匹配的正则对象 , ^代表从开头匹配,[0 ...

  6. python datetime模块strptime/strptime format常见格式命令_施罗德_新浪博客

    python datetime模块strptime/strptime format常见格式命令_施罗德_新浪博客     python datetime模块strptime/strptime form ...

  7. Python datetime模块的datetime类

    datetime模块定义了下面这几个类: datetime.date:表示日期的类.常用的属性有year, month, day. datetime.time:表示时间的类.常用的属性有hour, m ...

  8. python处理时间--- datetime模块

    1   Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供的接口与C标准库time.h基本一致.相比于tim ...

  9. Python,datetime模块实例

    Python的标准模块datetime模块,在我们的工作中应用非常频繁,下面对datetime中常用的方法进行了总结和测试:对每一个方法都使用了单元测试框架Unittest来配合测试. 主要的类型有: ...

  10. python3 time模块与datetime模块

    time模块 在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素.由于Python的time模块实现主要调用C库,所以各个平 ...

随机推荐

  1. Netty(一):初识Netty

    Netty是什么? Netty是由JBOSS提供的一个java开源框架. Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络服务器和客户端程序. 封装了JDK底 ...

  2. eclipse进行远程调试教程,轻松搞定生产环境问题

    首先你本地Eclipse上要有和部署在远程服务器一至的项目,否则debug的时候会出现代码行错位,难以达到debug的效果.例如:如果你本地的代 码加了行,修改了,或减了一行.而远程服务器上的项目没有 ...

  3. 167. Two Sum II - Input array is sorted【easy】

    167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...

  4. C 调用 lua 函数

    C 调用 lua 函数 需要考虑的问题: 1. 使用 lua_pcall 可以调用 lua 函数,首先把 lua 函数入栈,然后把参数入栈, lua_pcall(luaState, 参数个数, 返回值 ...

  5. url传递参数

    url:'/randowCode?t='+Math.random(); //当给某个赋值可以: $('#change').click(function(){ $("#codeimage&qu ...

  6. docker下部署spring boot

    第 5 章 Docker + Spring Boot: 快速搭建和部署Java Web应用 0.你需要: JDK 1.8 : java -version Maven 3.0+ : mvn -v Git ...

  7. linux ls命令按时间显示文件

      本文介绍下,使用ls命令显示文件,并按时间排序的方法,供大家学习参考. 在linux系统中,使用ls命令按时间排序文件,其实很简单,如下: #ls -tr 即可按时间排序当前目录下的文件. 附,l ...

  8. StoryBoard不使用AutoLayout情况下 按比例快速兼容适配iPhone6/6 Plus教程【转载】

    StoryBoard不使用AutoLayout情况下 按比例快速兼容适配iPhone6/6 Plus教程[转] 声明:本文章是为了后期快速兼容6和6Plus的按比例放大方法,对于部分读者来说可能觉得该 ...

  9. openfile

    linux修改open files数   概要 linux系统默认open files数目为1024, 有时应用程序会报Too many open files的错误,是因为open files 数目不 ...

  10. tomcat 测试页面显示

    首先下载匹配jdk版本的tomcat 解压即可使用 将完成的html文件直接放置到webapps目录下的子目录中是无法使用的 原因是tomcat默认加载的是jsp文件,且需要文件配置 所以,除去在we ...