strftime和strptime函数均来自包datetime

from datetime import *

strftime:

将datetime包中的datetime类,按照入参格式生成字符串变量

from datetime import *
currenttime=datetime.now() #生成当前时间的datetime类实例
print('type of currenttime', type(currenttime))
print(currenttime)
cur=currenttime.strftime('%Y_%m_%d-%H-%M-%S')
print('type of cur', type(cur))
print(cur)

输出

type of currenttime <class 'datetime.datetime'>
2019-08-29 14:01:39.973547
type of cur <class 'str'>
2019_08_29-14-01-39

strptime:

将字符串根据其格式,提取所含时间,并生成datetime类实例

from datetime import *
strdate='2019:08:29:09-00-00'
strdatetime=datetime.strptime(strdate,'%Y:%m:%d:%H-%M-%S')
print('type of strdatetime:',type(strdatetime),':',strdatetime)

输出

type of strdatetime: <class 'datetime.datetime'> : 2019-08-29 09:00:00

注意:strptime中的第二个参数是输入字符串的格式,而不是出参格式,datetime实例的格式是固定的

python中strftime和strptime函数的更多相关文章

  1. Python 函数式编程 & Python中的高阶函数map reduce filter 和sorted

    1. 函数式编程 1)概念 函数式编程是一种编程模型,他将计算机运算看做是数学中函数的计算,并且避免了状态以及变量的概念.wiki 我们知道,对象是面向对象的第一型,那么函数式编程也是一样,函数是函数 ...

  2. Python中的高阶函数与匿名函数

    Python中的高阶函数与匿名函数 高阶函数 高阶函数就是把函数当做参数传递的一种函数.其与C#中的委托有点相似,个人认为. def add(x,y,f): return f( x)+ f( y) p ...

  3. python中enumerate()函数用法

    python中enumerate()函数用法 先出一个题目:1.有一 list= [1, 2, 3, 4, 5, 6]  请打印输出:0, 1 1, 2 2, 3 3, 4 4, 5 5, 6 打印输 ...

  4. Python中str()与repr()函数的区别——repr() 的输出追求明确性,除了对象内容,还需要展示出对象的数据类型信息,适合开发和调试阶段使用

    Python中str()与repr()函数的区别 from:https://www.jianshu.com/p/2a41315ca47e 在 Python 中要将某一类型的变量或者常量转换为字符串对象 ...

  5. Python中sort和sorted函数代码解析

    Python中sort和sorted函数代码解析 本文研究的主要是Python中sort和sorted函数的相关内容,具体如下. 一.sort函数 sort函数是序列的内部函数 函数原型: L.sor ...

  6. Python中进制转换函数的使用

    Python中进制转换函数的使用 关于Python中几个进制转换的函数使用方法,做一个简单的使用方法的介绍,我们常用的进制转换函数常用的就是int()(其他进制转换到十进制).bin()(十进制转换到 ...

  7. Python中的内置函数

    2.1 Built-in Functions The Python interpreter has a number of functions built into it that are alway ...

  8. Python中str()与repr()函数的区别

    在 Python 中要将某一类型的变量或者常量转换为字符串对象通常有两种方法,即str()或者 repr() . >>> a = 10 >>> type(str(a ...

  9. python中os.path.isdir()函数的使用

    在python 中,os.path.isdir(path)函数主要用来判断函数内部的path是否为一个目录 具体关于这个函数的解说参考博客https://blog.csdn.net/xjp_xujip ...

随机推荐

  1. 常量const实践

    这篇文章写的很好:https://www.cnblogs.com/zhangfeionline/p/5882790.html 自己是实践: 1. 定义时必须初始化值,不然如下错误: 2. 3. 使用:

  2. 从 sourcemap 中获取源码

    使用 paazmaya/shuji: Reverse engineering JavaScript and CSS sources from sourcemaps 可以从 sourcemap 中获取源 ...

  3. MAC截图工具

    截图快捷键 ctrl+shift+A

  4. Java中判断两个列表是否相等

    CollectionUtils.isEqualCollection(final Collection a, final Collection b) CollectionUtils工具类中有一个查看两个 ...

  5. nslookup的安装方法

    1.直接使用yum安装,没有找到:yum install nslookup 2.yum provides nslookup查询nslookup在哪个套件里面 3.根据上面的提示,在"*/ns ...

  6. pg和mysql对比

    作者:方圆链接:https://www.zhihu.com/question/20010554/answer/15863274来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...

  7. 梳理检测论文-Refinement Neural Network

    Single-Shot Refinement Neural Network for Object Detection 目录 1. motivation 2. RefineDet 解析(Network ...

  8. 不容错过的 MySQL史上最全

    点击下方链接 http://c.biancheng.net/view/2361.html

  9. Eclipse使用jdbc连接MySql数据库报:java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

    在使用eclipse连接mysql数据库时报异常: java.sql.SQLException: Access denied for user 'root'@'localhost' (using pa ...

  10. c++ 判断两圆位置关系

    对于两圆的位置一般有五种关系: (1) 外离:两圆的半径之和小于两圆圆心距离 (2) 外切:两圆的半径之和等于两圆圆心距离 (3) 相交:两圆的半径之和大于两圆圆心距离,两圆圆心距离大于两圆半径之差 ...