#/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. Codeforces Gym100814 B.Unlucky Teacher (ACM International Collegiate Programming Contest, Egyptian Collegiate Programming Contest (2015) Arab Academy for Science and Technology)

    今日份的训练题解,今天写出来的题没有昨天多,可能是因为有些事吧... 这个题就是老师改卷子,忘带标准答案了,但是他改了一部分卷子,并且确定自己改的卷子没出错,他想从改过的卷子里把标准答案推出来. 因为 ...

  2. (6)ASP.NET HttpServerUtility 类

    HttpServerUtility 类 提供用于处理 Web 请求的 Helper 方法 https://msdn.microsoft.com/zh-cn/library/system.web.htt ...

  3. Educational Codeforces Round 34 B. The Modcrab【模拟/STL】

    B. The Modcrab time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  4. [Math Review] Linear Algebra for Singular Value Decomposition (SVD)

    Matrix and Determinant Let C be an M × N matrix with real-valued entries, i.e. C={cij}mxn Determinan ...

  5. Python包管理工具pip的基本使用

    1.简介 pip 是一个Python包管理工具,主要是用于安装 PyPI 上的软件包,可以替代 easy_install 工具. 2.pip安装 如果你安装的Python 2 >=2.7.9 或 ...

  6. encodeURI 解码 编码

    var uriStr = "http://www.baidu.com?name=张三&num=001 zs"; var uriec = encodeURI(uriStr); ...

  7. 【java】TreeMap/HashMap的循环迭代中 keySet和entrySet和forEach方式 + map的几种迭代方式

    参考链接:https://www.cnblogs.com/crazyacking/p/5573528.html ================================== java紫色代表迭 ...

  8. 【lombok】lombok---帮你简化生成必要但臃肿的java代码工具 【映射注解和lombok注解同时使用 以及 映射注解放在属性和get方法上的区别】

    官方地址:https://projectlombok.org/ GitHub:https://github.com/rzwitserloot/lombok 指导说明文档:http://jnb.ociw ...

  9. 各种优化方法总结比較(sgd/momentum/Nesterov/adagrad/adadelta)

    前言 这里讨论的优化问题指的是,给定目标函数f(x),我们须要找到一组參数x.使得f(x)的值最小. 本文下面内容如果读者已经了解机器学习基本知识,和梯度下降的原理. SGD SGD指stochast ...

  10. git工程迁移(修改提交服务器地址)方法

    git remote set-url [--push] <name> <newurl> [<oldurl>]git remote set-url --add [-- ...