Python 从日期时间中提取时间并确定时间(不是日期)是否在范围内?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16138744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:51:37  来源:igfitidea点击:

Extract time from datetime and determine if time (not date) falls within range?

pythondatetimepython-2.6

提问by Dan

The problem is that I want it to ignore the date and only factor in the time. Here is what I have:

问题是我希望它忽略日期而只考虑时间。这是我所拥有的:

import time
from time import mktime
from datetime import datetime

def getTimeCat(Datetime):
    # extract time categories
    str_time = datetime.strptime(Datetime, "%m/%j/%y %H:%M")
    ts = datetime.fromtimestamp(mktime(str_time))

    # --> Morning = 0400-1000
    mornStart = datetime.time(4, 0, 1)
    mornEnd = datetime.time(10, 0, 0)

    # --> Midday = 1000-1600
    midStart = datetime.time(10, 0, 1)
    midEnd = datetime.time(16, 0, 0)

    # --> Evening = 1600-2200
    eveStart = datetime.time(16, 0, 1)
    eveEnd = datetime.time(22, 0, 0)

    # --> Late Night = 2200-0400
    lateStart = datetime.time(22, 0, 1)
    lateEnd = datetime.time(4, 0, 0)

    if time_in_range(mornStart, mornEnd, ts):
      timecat = 0 #morning
    elif time_in_range(midStart, midEnd, ts):
      timecat = 1 #midday
    elif time_in_range(eveStart, eveEnd, ts):
      timecat = 2 #evening
    elif time_in_range(lateStart, lateEnd, ts):
      timecat = 3 #late night

    return timecat

As is, I get this error:

按原样,我收到此错误:

TypeError: argument must be 9-item sequence, not datetime.datetime

When I change the relevant line to:

当我将相关行更改为:

str_time = time.strptime(Datetime, "%m/%j/%y %H:%M")

I get this error:

我收到此错误:

TypeError: descriptor 'time' requires a 'datetime.datetime' object but received a 'int'

I know I'm working with two different libraries or whatnot, but I'm not sure how to convert between them or accomplish what I want to do only using one. I just want it to ignore the date and only check if the time is within the specified ranges. Python 2.6 is a MUST due to a library I'm using elsewhere in the code.

我知道我正在使用两个不同的库或诸如此类的东西,但是我不确定如何在它们之间进行转换或仅使用一个来完成我想做的事情。我只是想让它忽略日期,只检查时间是否在指定范围内。Python 2.6 是必须的,因为我在代码的其他地方使用了一个库。

采纳答案by Austin Phillips

This line:

这一行:

str_time = datetime.strptime(Datetime, "%m/%j/%y %H:%M")

returns a datetimeobject as per the docs.

根据 docs返回一个datetime对象。

You can test this yourself by running the following command interactively in the interpreter:

您可以通过在解释器中以交互方式运行以下命令来自己测试:

>>> import datetime
>>> datetime.datetime.strptime('12/31/13 00:12', "%m/%j/%y %H:%M")
datetime.datetime(2013, 1, 31, 0, 12)
>>>

The time portion of the returned datetime can then be accessed using the .time()method.

然后可以使用该.time()方法访问返回的日期时间的时间部分。

>>> datetime.datetime.strptime('12/31/13 00:12', "%m/%j/%y %H:%M").time()
datetime.time(0, 12)
>>>

The datetime.time()result can then be used in your time comparisons.

datetime.time()然后可以将结果用于您的时间比较。

回答by aditya rao

Use this only gives you time.

使用它只会给你时间。

from datetime import datetime

now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("Current Time =", current_time)