简介:

记录一下 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. 《BAT前端进阶[师徒班]》学习总结

    这是一个培训课 是的,这是一个面向中级前端的培训班,但明显跟传统的填鸭式培训班不太一样.这边的老师都是大牛这是毫无疑问的,而且都是一线开发人员.而且课程一开始就说明了面向了是有1-3年有工作经验的前端 ...

  2. POJ 3278 Catch That Cow bfs 难度:1

    http://poj.org/problem?id=3278 从n出发,向两边转移,为了不使数字无限制扩大,限制在2*k以内, 注意不能限制在k以内,否则就缺少不断使用-1得到的一些结果 #inclu ...

  3. SGU 139. Help Needed! 逆序数,奇偶性,分析 难度:0

    139. Help Needed! time limit per test: 0.25 sec. memory limit per test: 4096 KB Little Johnny likes ...

  4. [CSS布局]3列布局:左右两列固定宽度、中间列自适应满宽

    一种常见的3列布局,左右两列固定宽度.中间列自适应满宽.整个网页不出现横向滚动条 纯CSS实现 效果图: 代码: <!DOCTYPE html> <html lang="e ...

  5. New Concept English Two 16 40

    Keynote Speech  are useful. $课文38  唯独没有考虑到天气 388. My old friend, Harrison, had lived in the Mediterr ...

  6. spring MVC 使用 modelAndView.setViewName("forward:*.action") 发送重定向

    1.Servlet重定向forward与redirect: 使用servlet重定向有两种方式,一种是forward,另一种就是redirect.forward是服务器内部重定向,客户端并不知道服务器 ...

  7. linux screen 命令详解(转载)

    转载于:http://www.cnblogs.com/mchina/archive/2013/01/30/2880680.html 一.背景 系统管理员经常需要SSH 或者telent 远程登录到Li ...

  8. /usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void

    /usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<std ...

  9. nginx 只容许域名访问禁止掉 ip 访问

    在原有 nginx server 的基础上再加上相同端口的配置 server {            listen 80 default_server;            server_name ...

  10. 解决 The word is not correctly spelled问题

    The word is not correctly spelled 此问题是eclipse校验单词拼写造成,如果出在配置文件中,一般会影响到程序的正常执行 解决方法:Window--Preferenc ...