原文链接:https://www.cnblogs.com/huhu-xiaomaomi/p/10338472.html

在python中,日期类型date和日期时间类型dateTime是不能比较的。

(1)如果要比较,可以将dateTime转换为date,date不能直接转换为dateTime

import datetime
dateTime_p = datetime.datetime.now()
date_p = dateTime_p.date()
print(dateTime_p) #2019-01-30 15:17:46.573139
print(date_p) #2019-01-30

(2)日期类型date转换为字符串str


#!/usr/bin/env python3
import datetime
date_p = datetime.datetime.now().date()
str_p = str(date_p)
print(date_p,type(date_p)) #2019-01-30 <class 'datetime.date'>
print(str_p,type(str_p)) #2019-01-30 <class 'str'>

(3)字符串类型str转换为dateTime类型

import datetime
str_p = '2019-01-30 15:29:08'
dateTime_p = datetime.datetime.strptime(str_p,'%Y-%m-%d %H:%M:%S')
print(dateTime_p) # 2019-01-30 15:29:08

(4)dateTime类型转为str类型

  这个地方我也不太理解,为什么指定格式无效

import datetime
dateTime_p = datetime.datetime.now()
str_p = datetime.datetime.strftime(dateTime_p,'%Y-%m-%d')
print(dateTime_p) # 2019-01-30 15:36:19.415157

(5)字符串类型str转换为date类型

#!/usr/bin/env python3
import datetime
str_p = '2019-01-30'
date_p = datetime.datetime.strptime(str_p,'%Y-%m-%d').date()
print(date_p,type(date_p)) # 2019-01-30 <class 'datetime.date'>

另外dateTime类型和date类型可以直接做加1减1这种操作

#!/usr/bin/env python3
import datetime
# today = datetime.datetime.today()
today = datetime.datetime.today().date()
yestoday = today + datetime.timedelta(days=-1)
tomorrow = today + datetime.timedelta(days=1)
print(today) # 2019-01-30
print(yestoday)# 2019-01-29
print(tomorrow)# 2019-01-31

python 日期、时间、字符串相互转换(转载)的更多相关文章

  1. Python 日期时间处理模块学习笔记

    来自:标点符的<Python 日期时间处理模块学习笔记> Python的时间处理模块在日常的使用中用的不是非常的多,但是使用的时候基本上都是要查资料,还是有些麻烦的,梳理下,便于以后方便的 ...

  2. 关于bat中日期时间字符串的格式化

    在其他编程语言中,要实现日期时间字符串的格式化,包括时间计算,都是比较简单的 但在bat或者说cmd.dos中要实现这些功能.还是有一定难度的 首先,windows的cmd中可以使用%date%表示日 ...

  3. Python日期时间函数处理

    所有日期.时间的 api 都在datetime模块内. 1 日期的格式化输出 datetime => string import datetime now = datetime.datetime ...

  4. 格式化日期时间字符串 Get-Date -Uformat , -format

    #将字符串格式化为时间格式 $dateTimeStr = '20141231T23:59:59' $format = 'yyyyMMddTHH:mm:ss' $formatProvider = [Gl ...

  5. 【转载】Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码

    本文转载自脚本之家,源网址为:https://www.jb51.net/article/147429.htm 一.Python中日期时间模块datetime介绍 (一).datetime模块中包含如下 ...

  6. 使用Python将字符串转换为格式化的日期时间字符串

    我正在尝试将字符串“20091229050936”转换为“2009年12月29日(UTC)” >>>import time >>>s = time.strptime ...

  7. python 日期 & 时间

    1. Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间. 2. 时间间隔是以秒为单位的浮点小数. 3. 每个时间戳都以自从1970年1月1日午夜(历元)经过了多长 ...

  8. Python日期时间Date/Time

    Python程序可以处理多种方式的日期和时间.日期格式之间的转换是一种常见计算机的杂活. Python的时间和日历模块,能帮助处理日期和时间. Tick是什么? 为时间间隔,以秒为单位的浮点数.从“新 ...

  9. 1、Python 日期时间格式化输出

    今天帮朋友写自动化脚本,又需要用格式化日期,又忘记怎么写了,还是写到自己博客里面,方便日后需要的时候看一眼吧.So,临时加一篇 Python 的文章. 1.Python的time模块 import t ...

  10. python日期时间处理

    time模块 #-*- coding: utf-8 -*- """ #获取当前时间的时间戳(单位秒) time.time() #推迟指定秒数后再运行 time.sleep ...

随机推荐

  1. 如何在Typora中使用流程图

    1.横向流程图源码格式: graph LR A[方形] -->B(圆角) B --> C{条件a} C -->|a=1| D[结果1] C -->|a=2| E[结果2] A- ...

  2. 信安周报-第03周:DB系统表

    信安之路 第03周 前言 这周自主研究的任务如下: 任务附录的解释: 文件读写在通过数据库注入漏洞获取webshell的时候很有用 系统库和表存放了很多关键信息,在利用注入漏洞获取更多信息和权限的过程 ...

  3. InstantiationAwareBeanPostProcessor 分析

    Cglib之Enhancer创建动态代理https://blog.csdn.net/yaomingyang/article/details/82762697 https://blog.csdn.net ...

  4. Ubuntu 下安装zsh和oh-my-zsh

    注意:安装前先备份/etc/passwd 一开始装oh-my-zsh我是拒绝的,因为这东西安装容易,卸载难,真的很难. Mac安装参考:http://www.cnblogs.com/EasonJim/ ...

  5. unable to retrieve container logs for docker kubernetes

    参考 https://github.com/knative/docs/issues/300 This is what happens when the build is successful and ...

  6. 二.HTML

    1.HTML 1. <head></head>标签 <!DOCTYPE html> <!--统一规范--> <!----> <html ...

  7. drf--认证组件

    目录 认证简介 用户认证RBAC(Role-Based Access Control) 局部使用 全局使用 源码分析 认证简介 使用场景:有些接口在进行访问时,需要确认用户是否已经登录,比如:用户需要 ...

  8. IntelliJ Cannot find declaration to goto----解决方案

    系统中已经有了该类库,还是找不到类提示 close the project in intellij. close intellij. go to the project folder and dele ...

  9. JS this指向总结

    使用 JavaScript 开发的时候,很多开发者多多少少会被 this 的指向搞蒙圈,但是实际上,关于 this 的指向,记住最核心的一句话:哪个对象调用函数,函数里面的this指向哪个对象. 下面 ...

  10. Java 之 日期时间类

    一.Date类 1.概述 java.util.Date 类 表示特定的瞬间,精确到毫秒. 2.构造方法 public Date():分配Date对象并初始化此对象,以表示分配它的时间(精确到毫秒) p ...