python 时间字符串与日期转化

datetime.datetime.strptime(string, format)

根据指定的格式解析字符串为一个datetime类型。相当于datetime.datetime(*time.strptime(string, format)[0:6])。

In [1]: import datetime

In [2]: s = '2015-10-28 18:33'

In [3]: datetime.datetime.strptime(s, '%Y-%m-%d %H:%M')
Out[3]: datetime.datetime(2015, 10, 28, 18, 33) In [4]: import time In [5]: datetime.datetime(*time.strptime(s, '%Y-%m-%d %H:%M')[0:6])
Out[5]: datetime.datetime(2015, 10, 28, 18, 33)

格式化输出日期

  • datetime.datetime.strftime

    也可以使用time.strftime进行格式化输出。
In [6]: dt = datetime.datetime(*time.strptime(s, '%Y-%m-%d %H:%M')[0:6])

In [9]: dt.strftime('%Y/%m/%d %H:%M')
Out[9]: '2015/10/28 18:33'
  • 字符串格式化
In [10]: '{:%Y%m%d %H:%M}'.format(dt)
Out[10]: '20151028 18:33'

python 时间字符串与日期转化的更多相关文章

  1. python 时间字符串和时间戳之间的转换

    https://blog.csdn.net/qq_37193537/article/details/78987949   1.将字符串的时间转换为时间戳    方法:        a = " ...

  2. Python之字符串转换为日期、结合时区的日期操作

    一.字符串转换为日期 方法一 s = '2019-01-20' print(datetime.strptime(s, '%Y-%m-%d')) # 2019-01-20 00:00:00 方法二 de ...

  3. python 时间和时间戳的转化

    时间戳与时间之间的转换,需要一个中间过程,即将先将时间或时间戳先转为时间元组! 1.时间转时间戳: import datetime, time s = datetime.datetime(2016,6 ...

  4. int型时间字符串转日期

    string re ="201611"; DateTime d1 = DateTime.ParseExact(re, "yyyyMMdd", null);

  5. Python时间日期格式化之time与datetime模块总结

    1 引言 在实际开发过程中,我们经常会用到日期或者时间,那么在Python中我们怎么获取时间,以及如何将时间转换为我们需要的格式呢?在之前的开发中,也曾遇到time.datetime等模块下的不同函数 ...

  6. Python时间日期格式化之time与datetime模块

    1 引言 在实际开发过程中,我们经常会用到日期或者时间,那么在Python中我们怎么获取时间,以及如何将时间转换为我们需要的格式呢?在之前的开发中,也曾遇到time.datetime等模块下的不同函数 ...

  7. Python的time(时间戳与时间字符串互相转化)

    strptime("string format")字符串如"20130512000000"格式的 输入处理函数 localtime(float a)时间戳的输入 ...

  8. python时间日期字符串各种

    python时间日期字符串各种 第一种 字符串转换成各种日期 time 库 # -*- coding: utf-8 -*- import time, datetime # 字符类型的时间 tss1 = ...

  9. Python 时间推进器-->在当前时间的基础上推前n天 | CST时间转化标准日期格式

    由于公司任务紧迫,好久没有在园子里写自己的心得了,今天偷个闲发表点简单的代码块,在开源的时代贡献微薄力量.话不多说,直接上代码块: ]) m = ]) d = ]) the_date = dateti ...

随机推荐

  1. CRM Entity 之Money转string int类型等

    Money转string 左右都是string //服务站地址 vehicleDetail["yt_servicestation_address"]=serviceStationC ...

  2. SQL游标+递归查询客户子客户转换率

    ALTER PROCEDURE [dbo].[Account3YearsConversion ] as DECLARE @AccountId UNIQUEIDENTIFIER , @yearbefor ...

  3. Linux Mysql 总结

    一:Error Code: . Access denied for user 'root'@'%' to database ① mysql -u root -p 进入到mysql中 ②SELECT h ...

  4. Twisted No module named win32api

    安装twisted成功后,使用时抛错: No module named win32api 解决方案,需要安装 pywin32 下载地址: https://sourceforge.net/project ...

  5. java面向对象之 多态 Polymorphism

    多态(Polymorphism):用我们通俗易懂的话来说就是子类就是父类(猫是动物,学生也是人),因此多态的意思就是:父类型的引用可以指向子类的对象. 1.多态的含义:一种类型,呈现出多种状态 主要讨 ...

  6. silverlight imagesource赋值与转换

    介绍几种常用的Image source 赋值方式: this.abc.Source = new BitmapImage(new Uri("/1.jpg", UriKind.Rela ...

  7. python之字符串格式化(format)

    用法: 它通过{}和:来代替传统%方式 1.使用位置参数 要点:从以下例子可以看出位置参数不受顺序约束,且可以为{},只要format里有相对应的参数值即可,参数索引从0开,传入位置参数列表可用*列表 ...

  8. Lucky Sum

    Description Lucky Sum time limit per test: 2 seconds memory limit per test: 256 megabytes input: sta ...

  9. IOS 访问系统粘贴板

    粘贴板提供了一种核心OS特性,用于跨应用程序共享数据.用户可以跨应用来复制粘贴,也可以设置只在本应用中复制粘贴用来保护隐私. UIPasteboard类允许访问共享的设备粘贴板以及内容,下面代码返回一 ...

  10. COB (Chip On Board) 製程介紹/簡介/注意事項 II

    銀膠 (Silver glue) 如果晶圓有接地或是散熱需求時,一般都會採用[銀膠],如果沒有的話則會採用[厭氧膠].[厭氧膠]顧名思義就是阻隔它與空氣接觸後就會自然固化,不需要高溫烘烤.使用銀膠則需 ...