日期和时间主要有两个库,datetime和time.

datetime:
日期:datetime.date.today()
日期和时间:datetime.datetime.now()
1000天之后:datetime.datetimedelta(days=1000)
打印格式的问题:isoformat(),strftime()
字符串转换:strptime() time:
datetime.time(12,11,30)
time.time() 实际时间
time.clock() CPU时间
time.sleep() 休眠

  

各种不解释,直接代码中实践。

导入模块:

>>> import datetime

date获取今天日期:

>>> a=datetime.date.today()
>>> a
datetime.date(2015, 10, 20)
>>> a.day
20
>>> a.year
2015
>>> a.month
10

datetime获取今天日期:

>>> b=datetime.datetime.now()
>>> b
datetime.datetime(2015, 10, 20, 15, 54, 34, 345931)

1000天以后是什么时间:

timedelta是一个特殊类型的对象,它包含了一些天数,如果有必要的话,还会有一些秒数。我们可以使用其来增加或减少日期。

>>> a=datetime.date.today()
>>> d=datetime.timedelta(days=1000)
>>> (a+d).isoformat()
'2018-07-16'

显示更好看点:

>>> (a+d).strftime('%m/%d/%Y')
'07/16/2018'

1000个小时后是什么时间

>>> e=datetime.timedelta(hours=1000)
>>> b=datetime.datetime.now()
>>> (e+b).isoformat()
'2015-12-01T08:15:20.952474'

距离去年开学多少天了

>>> sad_day=datetime.datetime.strptime('2014-09-15','%Y-%m-%d')
>>> b=datetime.datetime.now()
>>> print b-sad_day
400 days, 16:23:09.111938

看看时光倒流了没

>>> sad_day>b
False

提前设置个下班时间:

>>> print datetime.time(12,11,30)
12:11:30

测试两段程序哪个运行速度快

import time

a=input("please input 0 or 1:")

start_time = time.time()
start_clock = time.clock() if a:
sum_i=0
for i in range(100000):
sum_i+=i
else:
sum_i=sum(range(100000)) print sum_i time.sleep(2)
end_time = time.time()
end_clock = time.clock() print "time-delta:"
print start_time-end_time
print "clock-delta:"
print start_clock-end_clock

运行结果:

[root@lxd python]# python date.py
please input 0 or 1:0
4999950000
time-delta:
-2.0064599514
clock-delta:
-0.01
[root@lxd python]# python date.py
please input 0 or 1:1
4999950000
time-delta:
-2.02046704292
clock-delta:
-0.01

time.time()   程序运行的实际时间

time.clock()   cpu时间

time.sleep() 以秒为单位的休眠时间

 

Python日期和时间的更多相关文章

  1. Python 日期和时间(转)

    Python 日期和时间 Python程序能用很多方式处理日期和时间.转换日期格式是一个常见的例行琐事.Python有一个 time 和 calendar 模组可以帮忙. 什么是Tick? 时间间隔是 ...

  2. (转)Python 日期和时间

    转自http://www.runoob.com/python/python-date-time.html Python 日期和时间 Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见 ...

  3. Python 日期和时间 —— datetime

    Python 日期和时间 —— datetime Python提供了多个内置模块用于操作日期时间,如calendar,time,datetime.calendar用于处理日历相关 :time提供的接口 ...

  4. 【310】◀▶ Python 日期和时间

    参考: python 时间日期计算 Python 日期和时间(菜鸟教程) 8.1. datetime — Basic date and time types python中datetime模块中dat ...

  5. python 日期、时间、字符串相互转换

    python 日期.时间.字符串相互转换 在python中,日期类型date和日期时间类型dateTime是不能比较的. (1)如果要比较,可以将dateTime转换为date,date不能直接转换为 ...

  6. Python 日期和时间_python 当前日期时间_python日期格式化

    Python 日期和时间_python 当前日期时间_python日期格式化 Python程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能. Python 提供了一个 time 和 cal ...

  7. Python日期和时间_什么是Tick_什么是时间元组_获取当前时间

    Python 日期和时间_什么是 Tick _什么是时间元组: 时间和日期:某年某月某日某时某分某秒 Tick: 时间间隔以 秒 为单位的浮点小数,起始时间为:1970年1月1日0点0分开始 # Ti ...

  8. 【转】Python 日期和时间

    本文转自:http://www.runoob.com/python/python-date-time.html Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能. Pytho ...

  9. Python 日期和时间

    Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能. Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间. 时间间隔是以秒为单位的浮点小数. ...

  10. Python 日期和时间操作

    Python提供了一个time 和calendar模块可以用于格式化日期和时间. 时间间隔是以秒为单位的浮点小数. 每个时间戳都是以自从1970年1月1日午夜(历元)经过了多长时间来表示. Pytho ...

随机推荐

  1. acd - 1403 - Graph Game(博弈 + 二分图最大匹配)

    题意:N与P在玩游戏,N有 n1 个点,P有 n2 个点,N的点与P的点之间有 m 条无向边.将一个石子放在当中一点.N先移动石子.沿边移动一次,石子移动前的点及与该点相连的边被删除.接着到P移动石子 ...

  2. web-小知识点(随记)

    1.position:fixed:窗口定位的时候(在刷新页面时若未出现.得稍等一会的话,就直接把需要定位的内容写在<html>标签的首部.这样页面加载html的时候首先加载fixed定位的 ...

  3. C#代码用法

    1.new的用法using System;using System.Collections.Generic;using System.Text;namespace yanz{public class ...

  4. IPython introduction

    转载:http://blog.csdn.net/gavin_john/article/details/53086766 1. IPython介绍 ipython是一个python的交互式shell,比 ...

  5. MySQL中in(常量列表)的执行计划

    我们在写sql的时候,经常用到in,in后面跟一堆常量列表,如id.有人说in的效率很高,而有人说很低:有人说in能使用索引,还有人说in不能使用索引... 到底是一个怎样的情况呢?我们分析以下几种情 ...

  6. centos7 配置ssh 免密码登陆

    我只有一台机器,是因为要配置hadoop分布式环境用,需要配置ssh 两个用户: zhangxs, root 首先在切换到zhangxs用户下 执行[ ssh-keygen -t rsa] [zhan ...

  7. spring+springMVC+hibernate整合

    首先我们要知道hibernate五大对象:,本实例通过深入的使用这五大对象和spring+springMVC相互结合,体会到框架的好处,提高我们的开发效率 Hibernate有五大核心接口,分别是:S ...

  8. macOS 安装安卓模拟器 并用charles抓包

    mac上面安装安卓模拟器并能使用charles抓包软件调研 一.Genymotion 1.先下载Virtua Box虚拟机 https://www.virtualbox.org/wiki/Downlo ...

  9. rsync 删除大量小文件

    3 find with delete 复制代码代码如下:  test  time find ./ -type f -delete        find ./ -type f -delete  0.4 ...

  10. android 布局特点

    <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_cont ...