python time用法
函数time.time()用于获取当前时间戳
获取当前时间
从返回浮点数的时间辍方式向时间元组转换,只要将浮点数传递给如localtime之类的函数。
#!/usr/bin/python # -*- coding: UTF-8 -*-import time localtime = time.localtime(time.time()) print "本地时间为 :", localtime
以上实例输出结果:
本地时间为 : time.struct_time(tm_year=2016, tm_mon=4, tm_mday=7, tm_hour=10, tm_min=3, tm_sec=27, tm_wday=3, tm_yday=98, tm_isdst=0)
获取格式化的时间
可以根据需求选取各种格式,但是最简单的获取可读的时间模式的函数是asctime():
#!/usr/bin/python # -*- coding: UTF-8 -*-import timelocaltime = time.asctime( time.localtime(time.time()) ) print "本地时间为 :", localtime以上实例输出结果: 本地时间为 : Thu Apr 7 10:05:21 2016
格式化日期
我们可以使用 time 模块的 strftime 方法来格式化日期,
time.strftime(format[, t])
#!/usr/bin/python # -*- coding: UTF-8 -*- import time# 格式化成2016-03-20 11:45:39形式 print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())# 格式化成Sat Mar 28 22:24:24 2016形式 print time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()) # 将格式字符串转换为时间戳 a = "Sat Mar 28 22:24:24 2016" print time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))以上实例输出结果: 2016-04-07 10:25:09 Thu Apr 07 10:25:09 2016 1459175064.0
转载于:https://blog.51cto.com/martinlei/1958675
python time用法相关推荐
- Python pandas用法
Python pandas用法 无味之味关注 12019.01.10 15:43:25字数 2,877阅读 91,914 介绍 在Python中,pandas是基于NumPy数组构建的,使数据预处理. ...
- python goto 用法
python goto 用法 pip install goto-statement from goto import with_goto @with_goto def range(start, sto ...
- Python SQLite 用法
Python SQLite 用法 具体可以参考网址 代码: #导入 import sqlite3 #连接库,如果数据库不存在,那么它就会被创建,最后将返回一个数据库对象. # test.db:数据路路 ...
- import的用法python_Python导入模块,Python import用法(超级详细)
Python导入模块,Python import用法(超级详细) 使用 Python 进行编程时,有些功能没必须自己实现,可以借助 Python 现有的标准库或者其他人提供的第三方库.比如说,在前面章 ...
- python with用法
@python with用法 python中with可以明显改进代码友好度,比如: [python] view plaincopyprint? with open('a.txt') as f: pri ...
- Python高级用法总结
Python很棒,它有很多高级用法值得细细思索,学习使用.本文将根据日常使用,总结介绍Python的一组高级特性,包括:列表推导式.迭代器和生成器.装饰器. 列表推导(list comprehensi ...
- 数据库和python的结合_redis数据库及与python交互用法简单示例
本文实例讲述了redis数据库及与python交互用法.分享给大家供大家参考,具体如下: redis数据操作 1.string类型:主要存储字符串 操作 命令 设置键值 set key value 设 ...
- python输出函数是什么_Python中打印函数使用概述,python,print,用法,总结
输出的 print 函数总结: 1. 字符串和数值类型 可以直接输出 print(1) 1 print("Hello World") Hello World 2.变量 无论什么类型 ...
- Python break用法详解
文章目录 Python break 用法 Python break 用法 我们知道,在执行 while 循环或者 for 循环时,只要循环条件满足,程序将会一直执行循环体,不停地转圈.但在某些场景,我 ...
- python基本用法_Python基本用法总结
Python 的设计具有很强的可读性,易于使用.这里对python基本用法做一个总结介绍. 一. 变量类型 1. 变量赋值 Python中的变量赋值不需要类型声明,每个变量在使用前都必须赋值,变量赋值 ...
最新文章
- git 查看修改的代码行数,新增的文件以及删除的代码行数
- mongodb 查看数据库和空间大小
- TCPDUMP/LIBPCAP 3-PCAP's MAN手册(1)
- Linux内核:进程上下文切换
- 定时备份为Sharepoint做网站备份,并删除指定日期的备份
- 软件设计师10-面向对象-设计模式
- poj 3017 Cut the Sequence(DP+单调队列)
- 阿里云服务器定时跑python_通过python调用阿里api定时备份阿里云RDS
- 使用maven启动web项目报错
- 【个人笔记】OpenCV4 C++ 快速入门 29课
- python数据结构与算法第10讲_Python数据结构与算法10:基本结构:练习题1
- MATLAB中figure函数
- excel生成随机手机号
- 104道精选iOS面试题PDF
- Ffmpeg 微信amr转换Mp3
- 查看电脑安装的Office是32位还是64位的
- 开机黑屏显示html,电脑开机黑屏只显示光标怎么办?来看看几种原因分析及解决方法!...
- PHP 服务器变量$_SERVER 服务器环境变量 $_ENV
- linux下根据关键字批量杀掉进程
- 用科学计算机打游戏,10个惊人的游戏,竟然有助于科学研究,你玩过几个?