GVKun编程网logo

如何将增量添加到python datetime.time?(python中增量赋值)

7

在本文中,我们将带你了解如何将增量添加到pythondatetime.time?在这篇文章中,我们将为您详细介绍如何将增量添加到pythondatetime.time?的方方面面,并解答python中

在本文中,我们将带你了解如何将增量添加到python datetime.time?在这篇文章中,我们将为您详细介绍如何将增量添加到python datetime.time?的方方面面,并解答python中增量赋值常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的02-05 Python库-time datetime、datetime.datetime.strptime在Python 2.4.1中不存在、Jquery Datetimepicker到Python Datetime、python date 和 datetime 的取值范围(对比 Mysql 的 datetime 和 timestamp)

本文目录一览:

如何将增量添加到python datetime.time?(python中增量赋值)

如何将增量添加到python datetime.time?(python中增量赋值)

从:

http://docs.python.org/py3k/library/datetime.html#timedelta-
objects

timedelta对象代表持续时间,即两个日期或时间之间的差。

那么,为什么我会出错:

>>> from datetime import datetime, timedelta, time>>> datetime.now() + timedelta(hours=12)datetime.datetime(2012, 9, 17, 6, 24, 9, 635862)>>> datetime.now().date() + timedelta(hours=12)datetime.date(2012, 9, 16)>>> datetime.now().time() + timedelta(hours=12)Traceback (most recent call last):  File "<stdin>", line 1, in <module>TypeError: unsupported operand type(s) for +: ''datetime.time'' and ''datetime.timedelta''

答案1

小编典典

datetime.time对象不支持除了用datetime.timedelta秒。

虽然有一个自然的定义,时钟算术。您可以这样计算:

import datetime as dtnow = dt.datetime.now()delta = dt.timedelta(hours = 12)t = now.time()print(t)# 12:39:11.039864print((dt.datetime.combine(dt.date(1,1,1),t) + delta).time())# 00:39:11.039864

dt.datetime.combine(...)将datetime.timet提升到一个datetime.datetime对象,然后添加增量,然后将结果回落到一个datetime.time对象。

02-05 Python库-time datetime

02-05 Python库-time datetime

time

Python 日期和时间

主要的常用方式:

  1. 获取时间戳(参考上面链接)
  2. 时间的格式化,用户生成测试报告时命名(参考上面链接)
  3. 获取今天,过去某天,未来某天的日期
import datetime


def get_today_date():
    """
    获取今天的日期
    :return:
    """
    today_date = datetime.date.today()
    return today_date

def get_pass_date(interval_day):
    """
    获取过去的日期
    :param interval_day: 间隔的天数
    :return:
    """
    today_date = datetime.date.today()
    pass_date = today_date + datetime.timedelta(days=-interval_day)
    return pass_date

def get_future_date(interval_day):
    """
    获取未来的日期
    :param interval_day: 间隔的天数
    :return:
    """
    today_date = datetime.date.today()
    future_date = today_date + datetime.timedelta(days=+interval_day)
    return future_date

datetime.datetime.strptime在Python 2.4.1中不存在

datetime.datetime.strptime在Python 2.4.1中不存在

在某些情况下,我们的团队需要使用Python 2.4.1。在Python
2.4.1strptime中的datetime.datetime模块中不存在:

Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)]Type "help", "copyright", "credits" or "license" for more information.>>> import datetime>>> datetime.datetime.strptimeTraceback (most recent call last):  File "<string>", line 1, in <fragment>AttributeError: type object ''datetime.datetime'' has no attribute ''strptime''

与2.6相反:

Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on win32Type "help", "copyright", "credits" or "license" for more information.>>> import datetime>>> datetime.datetime.strptime<built-in method strptime of type object at 0x1E1EF898>

键入时,我在2.4.1的时间模块中找到了它:

Python 2.4.1 (#65, Mar 30 2005, 09:16:17) [MSC v.1310 32 bit (Intel)]Type "help", "copyright", "credits" or "license" for more information.>>> import time>>> time.strptime<built-in function strptime>

我认为它strptime在某个时候动了吗?检查这种事情的最好方法是什么。我尝试查看python的发行历史,但找不到任何东西。

答案1

小编典典

请注意,即使在2.7.1及更高版本中,该strptime功能仍在time模块中datetime

但是,如果您查看最新版本的datetime文档,则会在strptime以下位置看到此内容:

这相当于 datetime(*(time.strptime(date_string, format)[0:6]))

因此您可以改用该表达式。请注意,同一条目还显示“ 2.5版的新功能”。

Jquery Datetimepicker到Python Datetime

Jquery Datetimepicker到Python Datetime

我在jQuery Datetimepicker下面

<input type="text" name="datetime" id="datetime">
$("#datetime").datetimepicker({
    controlType: 'select',oneLine: true,timeFormat: 'hh:mm tt'
});

我将日期作为AJAX POST请求发送给Django:

const starts = $('#datetime').val();
$.ajax({
    type: 'POST',url: '/booking/new/',data: {
        starts: starts,},};

在Django我得到的价值:

if request.method == 'POST':
    starts = request.POST.get('starts')
    object.starts = datetime.strptime(starts,'%x %I:%M %p')

我收到以下错误:

ValueError: time data ’11/12/2016 12:00 am’ does not match format ‘%x %I:%M %p’

我尝试了不同的格式,但我找不到正确的格式.我可以从字符串值生成格式如何?

解决方法

您可以在 this之后定义默认日期时间格式,如 this

python date 和 datetime 的取值范围(对比 Mysql 的 datetime 和 timestamp)

python date 和 datetime 的取值范围(对比 Mysql 的 datetime 和 timestamp)

Python 的情况

先来看看 date 类型吧!

In [1]: from datetime import date

In [2]: date.min
Out[2]: datetime.date(1, 1, 1)

In [3]: str(date.min)
Out[3]: ''0001-01-01''

In [4]: str(date.max)
Out[4]: ''9999-12-31''

可以看到 python 的 date 的取值范围是 0001-01-019999-12-31

再来看看 datetime 吧!

In [5]: from datetime import datetime

In [6]: str(datetime.min)
Out[6]: ''0001-01-01 00:00:00''

In [7]: str(datetime.max)
Out[7]: ''9999-12-31 23:59:59.999999''

可以看到 python 的 datetime 的取值范围是 0001-01-01 00:00:009999-12-31 23:59:59.999999

Mysql 的情况

来看看 mysql 的情况吧,下面的官方文档中的内容:

The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in ''YYYY-MM-DD'' format. The supported range is ''1000-01-01'' to ''9999-12-31''.
The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in ''YYYY-MM-DD hh:mm:ss'' format. The supported range is ''1000-01-01 00:00:00'' to ''9999-12-31 23:59:59''.

The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of ''1970-01-01 00:00:01'' UTC to ''2038-01-19 03:14:07'' UTC.

总结一下就是:

  • date 的取值范围:1000-01-019999-12-31
  • timestamp 的取值范围:1970-01-01 00:00:012038-01-19 03:14:07
  • datetime 的取值范围:1000-01-01 00:00:009999-12-31 23:59:59

参考资料:mysql 文档

但是需要注意:

Invalid DATE, DATETIME, or TIMESTAMP values are converted to the “zero” value of the appropriate type (''0000-00-00'' or ''0000-00-00 00:00:00''), if the SQL mode permits this conversion. The precise behavior depends on which if any of strict SQL mode and the NO_ZERO_DATE SQL mode are enabled; see Section 5.1.10, “Server SQL Modes”.

还有这部分:
图片.png

翻译成人话的意思,就是 myql 会把无效的 date 设为 0000-00-00,无效的 datetime 和 timestamp 设为 0000-00-00 00:00:00

比如 2022-13-35 就是一个无效的 date,当把这个值插入到 mysql 中的时候,开启了严格模式的 mysql 会报错,未开启严格模式的 mysql 会转为 0000-00-00 保存。

‼️ 所以我们使用 Python 从 mysql 读取时间类型的时候,一定要注意这个坑!因为 0000-00-00 没有办法转成 Python 的 date 类型,会报错的!‼️


顺便可以看看 Mysql 的 date、timestamp、datetime 各占用几个字节:

  • date 类型 3 字节
  • timestamp 类型 4 字节
  • datetime 类型 8 字节

图片.png

参考文章:Data Type Storage Requirements


关于 Mysql 插入的时间类型可以是 0000-00-00 这种格式的原因可以参考:MySQL Incorrect datetime value: ''0000-00-00 00:00:00''

在 Mysql8 中默认是不可以插入这种无效时间的

我们今天的关于如何将增量添加到python datetime.time?python中增量赋值的分享就到这里,谢谢您的阅读,如果想了解更多关于02-05 Python库-time datetime、datetime.datetime.strptime在Python 2.4.1中不存在、Jquery Datetimepicker到Python Datetime、python date 和 datetime 的取值范围(对比 Mysql 的 datetime 和 timestamp)的相关信息,可以在本站进行搜索。

本文标签: