在python中,通常用下面几种方式来表示时间:

  • 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.
  • 格式化的时间字符串(format string)
  • 结构化的时间(struct_time) :  struct_time元组共有9个元素(年,月,日,时,分,秒,一年中第几周,一年中第几天,夏令时)

# time模块里的部分特有函数

import time

# time.sleep(1)      #time.sleep(s)函数让程序进程暂停秒数,经常利用sleep函数来测试程序
# 暂停时的执行情况,如多个线程发生冲突时的情况 # time.clock() #将当前cpu处理器时间返回为以秒为单位的浮点数 time.strftime('下午%H: %M分:%S秒') time.time() #自纪元年以秒为单位返回浮点数的时间
import datetime
print(datetime.datetime.now()) print(datetime.datetime.fromtimestamp(time.time())) #时间戳直接转成日期格式 print(datetime.datetime.now() + datetime.timedelta(days=3)) #当期时间+3天
print(datetime.datetime.now() - datetime.timedelta(days=3)) #当前时间-3天
print(datetime.datetime.now() + datetime.timedelta(days=-3)) #当前时间-3天
print(datetime.datetime.now() + datetime.timedelta(days=3,hours=3)) #当前时间+3天+30分 #输出结果
2018-12-05 21:54:01.007084
2018-12-05 21:54:01.007084
2018-12-08 21:54:01.007084
2018-12-02 21:54:01.007084
2018-12-02 21:54:01.007084
2018-12-09 00:54:01.007084

strftime()方法和strptime()方法的时间日期格式化符号及所代表意思

  1, %y  代表两位数的年份表示

  2, %Y  代表四位数的年份表示

  3, %m  代表月份

  4, %M  代表分钟数

  5, %d  代表月内中的一天

  6, %H  代表24小时制小时数

  7, %I  代表12小时制小时数

  8, %S  代表秒

  9, %a  本地简化星期名称

  10, %A  本地完整星期名称

  11, %b  本地简化的月份名称

  12, %B  本地完整的月份名称

  13, %c  本地相应的日期表示和时间表示

  14, %j  年内的一天

  15, %p  本地AM或者PM

  16, %U  一年中的星期数,星期天为星期的开始

  

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. You Only Look Once: Unified, Real-Time Object Detection(翻译)

    0 - 摘要 我们提出了YOLO,一种新的物体检测方法.之前的物体检测工作是通过重新使用分类器来进行检测.相反,我们将对象检测抽象为一个回归问题,描述为以空间分隔的边界框和相关的类别概率.一个简单的神 ...

  2. 判断GPS是否开启&转到设置GPS界面

    /** * 判断GPS是否开启,GPS或者AGPS开启一个就认为是开启的 * @param context * @return true 表示开启 */ public static final boo ...

  3. bash常用指令(未完)

    #号注释1.man 2.cd 3.clear 4.alias 命令别名设置 区别命令别名和变量的区别,别名可以直接执行,本质是一个函数的索引 5.grep 查找字符串 grep [-acinv] [- ...

  4. 51nod1693 水群 最短路

    若A=K*B,若仅通过操作二:将B变换为A需要K步, 由算数基本定理可知:k=p1*p2*……pn(p为素数,且可能重复) 那么:将B转化为p1*B需要p1步,将p1*B转化为p1*p2*B需要p2步 ...

  5. POJ 1458 Common Subsequence 最长公共子序列

    题目大意:求两个字符串的最长公共子序列 题目思路:dp[i][j] 表示第一个字符串前i位 和 第二个字符串前j位的最长公共子序列 #include<stdio.h> #include&l ...

  6. Elasticsearch 5.4.3实战--环境搭建

    1. 选择搭建的服务器信息 $ uname -a Linux SA0124 2.6.32-642.11.1.el6.x86_64 #1 SMP Fri Nov 18 19:25:05 UTC 2016 ...

  7. Linux查看本机IP:curl cip.cc

    curl http://members.3322.org/dyndns/getip curl ip.6655.com/ip.aspx curl ifconfig.me curl icanhazip.c ...

  8. ARMV7-M数据手册---Part B :System Level Architecture---B3 System Address Map

    1.前言 2.The system address map 3.System Control Space (SCS) 4.The system timer, SysTick 5. Nested Vec ...

  9. appium+java报错之nodejs报错

    $ gulp(node:784) fs: re-evaluating native module sources is not supported. If you areusing the grace ...

  10. 一个简单的Loading控件

    实现效果如下: 使用方法: 在layout文件中添加以下代码: <com.example.jack.ui.widget.RingLoading android:layout_width=&quo ...