简介:

记录一下 Python 如何获取昨天、今天、明天时间及格式化。

1、今天

In [1]: import time

In [2]: print time.strftime('%Y.%m.%d', time.localtime(time.time()))
2017.02.16 In [2]: print time.strftime('%Y.%m.%d %H:%M:%S', time.localtime(time.time()))
2017.02.16 16:21:43 # 两种格式的今天日期 In [3]: print int(time.mktime(time.strptime(time.strftime('%Y-%m-%d', time.localtime(time.time())), '%Y-%m-%d')))
1487174400 In [4]: print int(time.mktime(time.strptime(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), '%Y-%m-%d %H:%M:%S')))
1487232839 # 凌晨时间戳(秒) 2017-02-16 00:00:00
# 当前时间戳(秒) 2017-02-16 16:13:59 In [5]: print int(time.mktime(time.strptime('2017-02-16', '%Y-%m-%d')))
1487174400 In [6]: print int(time.mktime(time.strptime('2017-02-16 23:59:59', '%Y-%m-%d %H:%M:%S')))
1487260799 # 字符串转时间戳 In [7]: print int(time.mktime(time.strptime(time.strftime('%Y-%m-%d', time.localtime(time.time())), '%Y-%m-%d'))) * 1000
1487174400000 # 凌晨时间戳(毫秒) In [8]: print time.strftime('%Y-%m-%d', time.localtime(1487174400))
2017-02-16 In [9]: print time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1487174400))
2017-02-16 00:00:00 # 时间戳转字符串

2、昨天

In [1]: from datetime import datetime, timedelta

In [2]: print (datetime.now() - timedelta(days=1)).strftime('%Y.%m.%d')
2017.02.15 In [3]: print (datetime.now() - timedelta(days=1)).strftime('%Y.%m.%d %H:%M:%S')
2017.02.15 16:22:50 # 两种格式的昨天日期

3、明天

In [1]: from datetime import datetime, timedelta

In [2]: print (datetime.now() + timedelta(days=1)).strftime('%Y.%m.%d')
2017.02.17

Python time、datetime的更多相关文章

  1. python time、datetime、random、os、sys模块

    一.模块1.定义模块:用来从逻辑上组织Python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件(文件名:test.py,对应的模块名:test)包:用来从逻辑上组织 ...

  2. Python (time、datetime、random、os、sys、shutil)模块的使用

    ######################################################### 模块time ################################### ...

  3. Python time、datetime、os、random、sys、hashlib、json、shutil、logging、paramiko、subprocess、ConfigParser、xml、shelve模块的使用

    文章目录: 1. time & datetime模块 2. os模块 3. random模块 4. sys模块 5. hashlib模块 6. json模块 7. shutil模块 8. lo ...

  4. python time、datetime模块

    时间的三种格式:1)时间戳 2)格式化的时间字符串 3)元组(struct_time):time.struct_time(tm_year=1970, tm_mon=5, tm_mday=23, tm_ ...

  5. Python之路(第十六篇)xml模块、datetime模块

    一.xml模块 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单, xml比较早,早期许多软件都是用xml,至今很多传统公司如金融行业的很多系统的接口还主要 ...

  6. python中time、datetime模块的使用

    目录 python中time.datetime模块的使用 1.前言 2.time模块 1.时间格式转换图 2.常用方法 3.datetime模块 python中time.datetime模块的使用 1 ...

  7. Python中的时间日期模块(time、datetime)

    目录 Datetime 获取当前时间 获取当前日期 获取当前时间的tuple元组 格式化日期和时间 时间移动 获取两个时间的时间差 时间格式转换 Time 获取距元年(1970.1.1)的秒数 当时时 ...

  8. python 中date datetime time 与str的互转

    以下全部引入 form datetime import datetime, timedelta import time 一.time 转str 二.datetime 转 str str_date = ...

  9. (九)random、os、time、datetime、hashlib、pymysql、excel、sys、string相关模块

    1.random模块 1 import random,string 2 print(string.printable) #代表所有的 数字+字母+特殊字符 3 4 print(random.randi ...

随机推荐

  1. python学习笔记(一)---python下载以及环境的安装

    转载网址:https://www.runoob.com/python/python-install.html 1.下载python安装包: 安装包下载网址(如下图所在的网址):https://www. ...

  2. 解决maven install报错信息(Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile )

    Maven install失败 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (d ...

  3. 《DSP using MATLAB》示例Example 9.9

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  4. lua 二进制函数使用

    由于 Lua 脚本语言本身不支持对数字的二进制操作(例如 与,或,非 等操作),MUSHclient 为此提供了一套专门用于二进制操作的函数,它们都定义在一个"bit"表中,使用时 ...

  5. C#调用OCR组件识别图片文字

    图片识别的技术到几天已经很成熟了,只是相关的资料很少,为了方便在此汇总一下(C#实现),方便需要的朋友查阅,也给自己做个记号. 图片识别的用途:很多人用它去破解网站的验证码,用于达到自动刷票或者是批量 ...

  6. PDO exec 执行时出错后如果修改数据会被还原?

    PDO exec 执行时出错后如果修改数据会被还原? 现象 FastAdmin 更新了 1127 版本,但是使用在线安装方式出现无法修改管理员密码的问题. 一直是默认的 admin 123456 密码 ...

  7. 【转】详解硬盘MBR

    原文网址:http://hi.baidu.com/waybq/item/a4490f026f9859d21ef046a4 硬盘是现在计算机上最常用的存储器之一.我们都知道,计算机之所以神奇,是因为它具 ...

  8. RK3288 device descriptor read/64, error -32

    CPU:RK3288 系统:Android 5.1 主板有两个USB接口,一个接USB摄像头,一个接身份证模块. 插入摄像头可以正常打开,再插入身份证模块时,摄像头就会卡主,而且身份证模块无法识别,内 ...

  9. 指向NULL的类

    引出:写个类A,声明类A指针指向NULL,调用类A的方法会有什么后果,编译通过吗,运行会通过吗? (在VS2008与VC++的情况下) 有错误欢迎批评指正! #include<stdio.h&g ...

  10. 转--NLTK的内置函数

    NLTK的内置函数 1. 词语索引 (1) concordance函数    给出一个指定单词每一次出现,连同上下文一起显示. >>>text1.concordance('monst ...