Python - 从日期时间字符串中删除时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26153795/
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-19 00:09:56  来源:igfitidea点击:

Python -Remove Time from Datetime String

pythonpython-2.7datetimestrptime

提问by pythonhunter

I have a date string and want to convert it to the date type:

我有一个日期字符串,想将其转换为日期类型:

I have tried to use datetime.datetime.strptime with the format that I want but it is returning the time with the conversion.

我试图使用 datetime.datetime.strptime 和我想要的格式,但它通过转换返回时间。

    when = alldates[int(daypos[0])]
    print when, type(when)

    then = datetime.datetime.strptime(when, '%Y-%m-%d')
    print then, type(then)

This is what the output returns:

这是输出返回的内容:

   2013-05-07 <type 'str'>
   2013-05-07 00:00:00 <type 'datetime.datetime'>

I need to remove the the time: 00:00:00.

我需要删除时间:00:00:00。

采纳答案by Woody Pride

print then.date()

What you want is a datetime.date object. What you have is a datetime.datetime object. You can either change the object when you print as per above, or do the following when creating the object:

你想要的是一个 datetime.date 对象。你有一个 datetime.datetime 对象。您可以在按上述方式打印时更改对象,也可以在创建对象时执行以下操作:

then = datetime.datetime.strptime(when, '%Y-%m-%d').date()

回答by Ignacio Vazquez-Abrams

>>> print then.date(), type(then.date())
2013-05-07 <type 'datetime.date'>

回答by Dovy

For me, I needed to KEEP a timetime object because I was using UTC and it's a bit of a pain. So, this is what I ended up doing:

对我来说,我需要保留一个 timetime 对象,因为我使用的是 UTC,这有点痛苦。所以,这就是我最终做的:

date = datetime.datetime.utcnow()

start_of_day = date - datetime.timedelta(
    hours=date.hour, 
    minutes=date.minute, 
    seconds=date.second, 
    microseconds=date.microsecond
)

end_of_day = start_of_day + datetime.timedelta(
    hours=23, 
    minutes=59, 
    seconds=59
)

Example output:

示例输出:

>>> date
datetime.datetime(2016, 10, 14, 17, 21, 5, 511600)
>>> start_of_day
datetime.datetime(2016, 10, 14, 0, 0)
>>> end_of_day
datetime.datetime(2016, 10, 14, 23, 59, 59)

回答by nagylzs

To convert a string into a date, the easiest way AFAIK is the dateutil module:

要将字符串转换为日期,AFAIK 最简单的方法是 dateutil 模块:

import dateutil.parser
datetime_object = dateutil.parser.parse("2013-05-07")

It can also handle time zones:

它还可以处理时区:

print(dateutil.parser.parse("2013-05-07"))
>>> datetime.datetime(2013, 5, 7, 1, 12, 12, tzinfo=tzutc())

If you have a datetime object, say:

如果您有一个日期时间对象,请说:

import pytz
import datetime
now = datetime.datetime.now(pytz.UTC)

and you want chop off the time part, then I think it is easier to construct a new object instead of "substracting the time part". It is shorter and more bullet proof:

并且你想砍掉时间部分,那么我认为构造一个新对象比“减去时间部分”更容易。它更短,更防弹:

date_part datetime.datetime(now.year, now.month, now.day, tzinfo=now.tzinfo)

It also keeps the time zone information, it is easier to read and understand than a timedelta substraction, and you also have the option to give a different time zone in the same step (which makes sense, since you will have zero time part anyway).

它还保留时区信息,它比 timedelta 减法更容易阅读和理解,并且您还可以选择在同一步骤中提供不同的时区(这是有道理的,因为无论如何您都会有零时间部分) .

回答by ukrutt

If you need the result to be timezone-aware, you can use the replace()method of datetimeobjects. This preserves timezone, so you can do

如果您需要时区感知结果,您可以使用对象的replace()方法datetime。这会保留时区,所以你可以这样做

>>> from django.utils import timezone
>>> now = timezone.now()
>>> now
datetime.datetime(2018, 8, 30, 14, 15, 43, 726252, tzinfo=<UTC>)
>>> now.replace(hour=0, minute=0, second=0, microsecond=0)
datetime.datetime(2018, 8, 30, 0, 0, tzinfo=<UTC>)

Note that this returns a newdatetime object -- nowremains unchanged.

请注意,这将返回一个新的日期时间对象——now保持不变。