#/usr/bin/python
#coding=utf-8
#@Time   :2017/11/9 8:55
#@Auther :liuzhenchuan
#@File   :时间格式的相互转换.py
 
import time
# datatime 是一个时间对象
from datetime import datetime
now_time = datetime.now()
print  now_time
print type(now_time)
 
#strftime() 是一个字符串类型,通过strftime 可以把时间格式转换成字符串类型
_time = now_time.strftime('%Y-%m-%d')
print type(_time)
 
 
#把字符串类型在转换成时间对象类型
#通过 datetime.strptime() 把时间字符串类型转换成时间对象
_dtime = datetime.strptime(_time,'%Y-%m-%d')
print type(_dtime)
 
 
 
#s时间戳,转换成时间对象
#时间戳:时间戳就是指格林或北京时间 1970年1月1日08时0分0秒起至今的总秒数
_a = time.time()
print _a
_n_time = datetime.fromtimestamp(_a)
print _n_time
print type(_n_time)
 
>>>
##########datetime 是一个时间对象类型##########
2017-11-09 10:13:25.830000
<type 'datetime.datetime'>
##########通过strftime 可以把时间格式转换成字符串类型##########
<type 'str'>
##########通过 datetime.strptime() 把时间字符串类型转换成时间对象##########
<type 'datetime.datetime'>
##########函数 datetime.fromtimestamp 把时间戳转换成时间对象##########
1510193605.83
2017-11-09 10:13:25.834000
<type 'datetime.datetime'>
 

python 基础 7.2 时间格式的相互转换的更多相关文章

  1. Python日志输出格式和时间格式

    formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s","%Y%b%d-%H:%M:% ...

  2. Python基础系列讲解——时间模块详解大全之time模块

    Python中提供处理时间日期相关的内置模块有time.datetime和calendar. time模块中大多数函数调用了所在平台C library 的同名函数,因此更依赖于操作系统层面,所以tim ...

  3. 1.4 Python基础知识 - 代码书写格式及条件判断"if ... else ..."

    一.代码的书写规则 在所有的开发语言中,代码之间都是有关联的关系,有的是包含关系,有的是上下级关系,有的是代表语句的结束.在python中也是有相应的规则的: 1.在没有上下级关系的代码中,代码要顶行 ...

  4. python解决json序列化时间格式

    简单实例 import json from datetime import datetime from datetime import date info = { "name": ...

  5. go 时间戳和时间格式的相互转换

    package main import( "fmt" "time" ) func main() { datetime := "2015-01-01 0 ...

  6. Java | 基础归纳 | java时间格式处理总结

    https://www.cnblogs.com/edwardlauxh/archive/2010/03/21/1918615.html https://blog.csdn.net/xsj_blog/a ...

  7. python获取hive表时间格式最大分区

    #获取表的最大分区 import boto3 from datetime import datetime,timedelta def get_max_partition(db_name,table_n ...

  8. python 日期、时间处理,各种日期时间格式/字符串之间的相互转换究竟是怎样的?

    模块函数说明 ''' date 日期对象,常用的属性有year,month,day time 时间对象,常用的属性有hour,minute,second,毫秒 datetime 日期时间对象,常用的属 ...

  9. Python基础学习笔记(十)日期Calendar和时间Timer

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-date-time.html 3. http://www.liao ...

随机推荐

  1. GIT 的常规操作

    GIT 的常规操作 常规操作也是我自己平时常用的几个命令, 学自于 pro git 这本书中 git 配置文件 git的配置文件位置 针对所有用户:/etc/gitconfig 针对当前用户: -/. ...

  2. Sprak RDD简单应用

    来自:http://my.oschina.net/scipio/blog/284957#OSC_h5_11 目录[-] 1.准备文件 2.加载文件 3.显示一行 4.函数运用 (1)map (2)co ...

  3. 洛谷——1968 美元汇率(DP)

    题目背景 此处省略maxint+1个数 题目描述 在以后的若干天里戴维将学习美元与德国马克的汇率.编写程序帮助戴维何时应买或卖马克或美元,使他从100美元开始,最后能获得最高可能的价值. 输入输出格式 ...

  4. mfoc安装编译

    系统环境 UBUNTU16.04,要安装好各种基础的编译环境,这就不说了,如果不知道需要什么,那就编译时出错时看到什么安装什么吧 下载源码包libnfc, mfoc, mfcuk,都是github上, ...

  5. Geoserver跨域问题

    1 Geoserver位war包,使用Tomcat发布的情况 由于geoserver为war形式,需要解决tomcat跨域问题,geoserver版本为2.9.1. (1)下载跨域war包 下载cor ...

  6. Linux基础学习3

    鸟哥私房菜第十一章   read 用来读取键盘输入的变量,常被用在shell script的交互当中 [root@www ~]# read [-pt] variable 选项不参数: -p :后面可以 ...

  7. tomcat使用安全及CVE-2017-12615

    tomcat安全情报的收集 1.首先定期查看官网各个版本存在的安全漏洞公告: http://tomcat.apache.org/security.html 2.去各大漏洞网站查看漏洞披露信息 看几个漏 ...

  8. 测试Apache服务器及httpd: Could not reliably determine the server's fully qualified domain name解决办法

    测试Apache服务器: 重启apache: sudo /usr/local/apache/bin/apachectl restart 若出现错误: httpd: Could not reliably ...

  9. vs2012搭建OpenGL环境

    1. 下载glut库 glut库地址为:http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip glut全称为:OpenGL ...

  10. linux下内存

    MMU由一个或一组芯片组成.其功能是把逻辑地址映射为物理地址,进行地址转换(MMU是CPU的一部分) 机器指令仍然用逻辑地址指定一个操作数的地址或一条指令的地址 每个逻辑地址都由一个段选择符(16位) ...