Python实现的当前时间多加一天、一小时、一分钟操作示例

yipeiwu_com7年前Python基础

本文实例讲述了Python实现的当前时间多加一天、一小时、一分钟操作。分享给大家供大家参考,具体如下:

首先看下,datetime的使用

>>> import datetime
>>> print datetime.datetime.now()
2017-07-15 15:01:24.619000

格式化时间

>>> print datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
2017-07-15 15:01:35
>>> print datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
2017-07-15 15:01
>>> print datetime.datetime.now().strftime("%Y%m%d")
20170715

多加一天

>>> print (datetime.datetime.now()+datetime.timedelta(days=1)).strftime("%Y-%m-%
d %H:%M:%S")
2017-07-16 15:12:42
>>>

多加一小时

>>> print (datetime.datetime.now()+datetime.timedelta(hours=1)).strftime("%Y-%m-
%d %H:%M:%S")
2017-07-15 16:10:43
>>>

多加一分钟

>>> print (datetime.datetime.now()+datetime.timedelta(minutes=1)).strftime("%Y-%
m-%d %H:%M:%S")
2017-07-15 15:12:56
>>>

PS:这里再为大家推荐几款关于日期与天数计算的在线工具供大家使用:

在线日期/天数计算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi

在线万年历日历:
http://tools.jb51.net/bianmin/wannianli

在线阴历/阳历转换工具:
http://tools.jb51.net/bianmin/yinli2yangli

Unix时间戳(timestamp)转换工具:
http://tools.jb51.net/code/unixtime

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python日期与时间操作技巧总结》、《Python数学运算技巧总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》

希望本文所述对大家Python程序设计有所帮助。

相关文章

Python检测生僻字的实现方法

解决思路 首先想到的就是利用 python 的正则表达式来匹配非法字符,然后找出非法记录。然而理想总是丰满的,现实却是残酷的。在实现的过程中,才发现自己对于字符编码、以及 python...

python使用logging模块发送邮件代码示例

logging模块不只是能记录log,还能发送邮件,使用起来非常简单方便 #coding=utf-8 ''''' Created on 2016-3-21 @author:...

python实现自动更换ip的方法

本文实例讲述了python实现自动更换ip的方法。分享给大家供大家参考。具体实现方法如下: #!/usr/bin/env python #-*- encoding:gb2312 -*...

详解python破解zip文件密码的方法

详解python破解zip文件密码的方法

1、单线程破解纯数字密码 注意: 不包括数字0开头的密码 import zipfile,time,sys start_time = time.time() def extract()...

python文件操作之批量修改文件后缀名的方法

1、引言 需要把.dat 格式 转化成 .txt格式 2、实现 ##python批量更换后缀名 import os # 列出当前目录下所有的文件 files = os.listdir...