python杂记-6(time&datetime模块)
#!/usr/bin/env python
# -*- coding: utf-8 -*- import time print(time.clock())##返回处理器时间,3.3开始已废弃 , 改成了time.process_time()测量处理器运算时间,不包括sleep时间,不稳定,mac上测不出来
print(time.process_time())
print(time.altzone)#返回与utc时间的时间差,以秒计算\
print(time.asctime())#返回时间格式“Thu Nov 3 10:04:39 2016”
print(time.localtime())#返回本地时间的struct time 对象格式
print(time.gmtime(time.time()-800000))#返回utc时间的struc时间对象格式 print(time.asctime(time.localtime()))#返回时间格式“Thu Nov 3 10:07:36 2016”
print(time.ctime())#返回Thu Nov 3 10:08:41 2016 #日期字符串转成 时间戳
string_2_struct = time.strptime("2016/05/22","%Y/%m/%d")#将日期字符串转成 struct时间对象格式
print(string_2_struct)
#
struct_2_stamp = time.mktime(string_2_struct)#将struct时间对象转成时间戳
print(struct_2_stamp) #将时间戳转为字符串格式
print(time.gmtime(time.time()-86640))#将utc时间戳转换成struct_time格式
print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()))#将utc struct_time格式转成指定的字符串格式 #时间加减
import datetime print(datetime.datetime.now())#返回 2016-11-03 10:51:04.385070
print(datetime.date.fromtimestamp(time.time()))#时间戳直接转成日期格式 2016-11-03
print(datetime.datetime.now()+datetime.timedelta(3))# 当前时间+3天
print(datetime.datetime.now()+datetime.timedelta(-3))# 当前时间-3天
print(datetime.datetime.now()+datetime.timedelta(hours=3))#当前时间+3小时
print(datetime.datetime.now()+datetime.timedelta(minutes=3))#当前时间+3分钟 #
c_time = datetime.datetime.now()
print(c_time.replace(minute=3,hour=2)) #时间替换
Directive | Meaning | Notes |
---|---|---|
%a |
Locale’s abbreviated weekday name. | |
%A |
Locale’s full weekday name. | |
%b |
Locale’s abbreviated month name. | |
%B |
Locale’s full month name. | |
%c |
Locale’s appropriate date and time representation. | |
%d |
Day of the month as a decimal number [01,31]. | |
%H |
Hour (24-hour clock) as a decimal number [00,23]. | |
%I |
Hour (12-hour clock) as a decimal number [01,12]. | |
%j |
Day of the year as a decimal number [001,366]. | |
%m |
Month as a decimal number [01,12]. | |
%M |
Minute as a decimal number [00,59]. | |
%p |
Locale’s equivalent of either AM or PM. | (1) |
%S |
Second as a decimal number [00,61]. | (2) |
%U |
Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. | (3) |
%w |
Weekday as a decimal number [0(Sunday),6]. | |
%W |
Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. | (3) |
%x |
Locale’s appropriate date representation. | |
%X |
Locale’s appropriate time representation. | |
%y |
Year without century as a decimal number [00,99]. | |
%Y |
Year with century as a decimal number. | |
%z |
Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59]. | |
%Z |
Time zone name (no characters if no time zone exists). | |
%% |
A literal '%' character. |
python杂记-6(time&datetime模块)的更多相关文章
- Python的time和datetime模块
Python的time和datetime模块 time 常用的有time.time()和time.sleep()函数. import time print(time.time()) 149930555 ...
- python中time、datetime模块的使用
目录 python中time.datetime模块的使用 1.前言 2.time模块 1.时间格式转换图 2.常用方法 3.datetime模块 python中time.datetime模块的使用 1 ...
- Python 入门之 内置模块 -- datetime模块
Python 入门之 内置模块 -- datetime模块 1.datetime模块 from datetime import datetime (1)datetime.now() 获取当前时间和日期 ...
- Python中time和datetime模块的简单用法
python中与时间相关的一个模块是time模块,datetime模块可以看为是time模块的高级封装. time模块中经常用到的有一下几个方法: time()用来获取时间戳,表示的结果为从1970年 ...
- python基础--time和datetime模块
一:说明在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素.由于Python的time模块实现主要调用C库,所以各个平台可能 ...
- [ Python入门教程 ] Python中日期时间datetime模块使用实例
Python中datetime模块提供强大易用的日期处理功能,用于记录程序操作或修改时间.时间计算.日志时间显示等功能.datatime模块重新封装了time模块,提供的类包括date.time.da ...
- python标准库:datetime模块
原文地址:http://www.bugingcode.com/blog/python_datetime.html datatime 模块题共用一些处理日期,时间和时间间隔的函数.这个模块使用面向对象的 ...
- Python之 time 与 datetime模块
time与datetime模块 一.time 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏 ...
- 【Python】Python的time和datetime模块
time 常用的有time.time()和time.sleep()函数. import time print(time.time()) 1499305554.3239055 上面的浮点数称为UNIX纪 ...
随机推荐
- ubuntu下安装redis
(1)进去 /usr/local目录下 cd /usr/local 若没有local这个文件夹则创建一个 sudo mkdir /usr/local sudo chmod 777 ...
- 基本java类-In.java
package com.algorithm.api; /************************************************************************ ...
- Flume与Kafka集成
一.Flume介绍 Flume是一个分布式.可靠.和高可用的海量日志聚合的系统,支持在系统中定制各类数据发送方,用于收集数据:同时,Flume提供对数据进行简单处理,并写到各种数据接受方(可定制)的能 ...
- poj 1695 动态规划
思路:和黑书上的跳舞机类似 #include<map> #include<set> #include<cmath> #include<queue> #i ...
- Entityframework Code First 系列之项目搭建
Entityframework(以下简称EF)是微软推出的一个ORM(Object Relational Mapping)框架. 优缺点 优点: 易上手,语法简单,查询容易 更新快,不断补足 缺点: ...
- html5技术介绍
什么是HTML5 1>网页的5.0版本 1> 2014年才定制完HTML5的标准,历时8年 2> 移动先行 为什么要用HTML5 1> 跨平台 利用HTML5编写的UI界面能运 ...
- Android OpenGL ES(三)----编程框架
首先当然是创建Android项目,你可以选择最新的Android Studio也可以选择eclipse都是一样的.我们重点讲解开发OpenGL ES的流程 1.定义顶点着色器和片段着色器 第一节我们讲 ...
- 如何判断单选按钮radio被选中
今天要写一个单选按钮选中之后显示某些内容,首要问题就是当选中如何获取到值! html代码如下 <input class="joined" type="radio&q ...
- Mapper映射器
在两个独立的对象之间建立通信的对象 需要在两个必须相互隔离的子系统间建立通信. 可能是因为无法修改已有的子系统,或者不愿意在两者之间建立依赖关系.甚至不愿意这两个子系统与另一个部件间建立依赖关系. 运 ...
- Linux下动态共享库加载及使用详解【转】
原文地址:http://blog.chinaunix.net/uid-29025972-id-3855500.html 对动态库的实际应用还不太熟悉的读者可能曾经遇到过类似“error while l ...