Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python';日期时间转换_Python_Python 2.7_Python Datetime - Fatal编程技术网

Python';日期时间转换

Python';日期时间转换,python,python-2.7,python-datetime,Python,Python 2.7,Python Datetime,这是我的密码: from datetime import datetime def get_local_time(time_str): """ takes a string in the format of '27 March at 3:00' which is UTC and converts it to local time and AM/PM :param time_str: """ offset = datetime.now() - da

这是我的密码:

from datetime import datetime

def get_local_time(time_str):
    """
    takes a string in the format of '27 March at 3:00' which is UTC
    and converts it to local time and AM/PM
    :param time_str:
    """
    offset = datetime.now() - datetime.utcnow()
    time_dt = datetime.strptime(time_str, '%d %b at %H:%M')
    return (time_dt + offset).strftime('%I:%M %p')
我遇到的问题是使用了一个只有时间的时间,不包括日/月。ie:“02:00”

如果我将其更改为:time_dt=datetime.strtime(time_str,“%H:%M”),那么我会得到一个关于strftime和1900年之前年份的错误


所以我在这里被难住了。需要做什么才能在输入字符串中留出一段时间?

您可以尝试使用
dateutil
包。输入字符串更改时,
parser.parse()
方法很好。如果字符串中只指定了时间,它将构造一个日期为todays date的datetime对象。它将处理各种其他格式

from datetime import datetime
from dateutil import parser

def get_local_time(time_str):
    """
    takes a string in the format of '27 March at 3:00' which is UTC
    and converts it to local time and AM/PM
    :param time_str:
    """
    offset = datetime.now() - datetime.utcnow()
    time_dt = parser.parse(time_str)
    return (time_dt + offset).strftime('%I:%M %p')

如果您仅限于datetime软件包,则可以执行以下操作:

from datetime import datetime

def get_local_time(time_str):
    """
    takes a string in the format of '27 March at 3:00' which is UTC
    and converts it to local time and AM/PM
    :param time_str:
    """
    if len(time_str) <= 5:
        time_str = datetime.now().strftime('%d %B at ') + time_str
    offset = datetime.now() - datetime.utcnow()
    time_dt = datetime.strptime(time_str, '%d %B at %H:%M')
    return (time_dt + offset).strftime('%I:%M %p')

print get_local_time('27 March at 3:00')
print get_local_time('3:00')
从日期时间导入日期时间
def get_local_时间(time_str):
"""
获取格式为“3月27日3:00”的字符串,即UTC
并将其转换为当地时间和AM/PM
:param time_str:
"""

如果len(time_str)我只是在repl中尝试了一下。这对我很有用:

>>> from datetime import datetime
>>> time = "02:00"
>>> time_dt = datetime.strptime(time, '%H:%M')
>>> time_dt
datetime.datetime(1900, 1, 1, 2, 0)
>>>
如果我没记错的话,datetime永远不会只存储一个时间,总会有一个虚拟日期1900年1月1日。如果要存储没有虚拟日期的时间,请尝试使用
time
类。它还具有strftime功能,请参见此处的文档:


如果datetime不起作用,您也可以尝试向它添加不同的虚拟日期。

我肯定会出错。这将与一个名为“Kodi”的应用程序一起使用,该应用程序使用python 2.7。所以我不确定这是怎么干扰的。但你也是对的,我可以加上一个虚拟的日期和月份,效果很好。看来我不该这么做。我猜这会很好用的。但我将此应用程序与另一个应用程序(Kodi)结合使用,因为dateutil似乎不是其中包含的库,所以它不起作用。除非有人有其他东西,否则仍将其标记为答案。@Bahnzo如果您不想要dateutil包,我会改进它。给我一点时间。它不起作用,但只是一个简单的改变。字符串的长度需要为5而不是4。所以:如果len(time_str)@Bahnzo糟糕,我忘了在第三段代码中将4改为5。如果你能看到的话,我在第二段中做得很正确。
>>> from datetime import datetime
>>> time = "02:00"
>>> time_dt = datetime.strptime(time, '%H:%M')
>>> time_dt
datetime.datetime(1900, 1, 1, 2, 0)
>>>