简体   繁体   English

Python datetime - 在使用 strptime 获取日、月、年之后设置固定的小时和分钟

[英]Python datetime - setting fixed hour and minute after using strptime to get day,month,year

I've successfully converted something of 26 Sep 2012 format to 26-09-2012 using:我已经成功地将26 Sep 2012格式转换为26-09-2012使用:

datetime.strptime(request.POST['sample_date'],'%d %b %Y')

However, I don't know how to set the hour and minute of something like the above to 11:59.但是,我不知道如何将上述内容的小时和分钟设置为 11:59。 Does anyone know how to do this?有谁知道如何做到这一点?

Note, this can be a future date or any random one, not just the current date.请注意,这可以是未来日期或任何随机日期,而不仅仅是当前日期。

Use datetime.replace :使用datetime.replace

from datetime import datetime
dt = datetime.strptime('26 Sep 2012', '%d %b %Y')
newdatetime = dt.replace(hour=11, minute=59)

datetime.replace() will provide the best options. datetime.replace()将提供最佳选择。 Also, it provides facility for replacing day, year, and month.此外,它还提供了替换日、年和月的功能。

Suppose we have a datetime object and date is represented as: "2017-05-04"假设我们有一个datetime对象,日期表示为: "2017-05-04"

>>> from datetime import datetime
>>> date = datetime.strptime('2017-05-04',"%Y-%m-%d")
>>> print(date)
2017-05-04 00:00:00
>>> date = date.replace(minute=59, hour=23, second=59, year=2018, month=6, day=1)
>>> print(date)
2018-06-01 23:59:59

If you have a date as a datetime.datetime (or a datetime.date ) instance and want to combine it via a time from a datetime.time instance, then you can use the classmethod datetime.datetime.combine :如果您有一个日期作为datetime.datetime (或datetime.date )实例并希望通过datetime.time实例中的时间将其组合,那么您可以使用 classmethod datetime.datetime.combine

import datetime
dt = datetime.datetime(2020, 7, 1)
t = datetime.time(12, 34)
combined = datetime.datetime.combine(dt.date(), t)

Shortcuts to convert datetime to max or min将日期时间转换为最大值或最小值的快捷方式

import datetime

def dt_time_min(dt):
    """converts any datetime/date to new datetime with same date and time=0:00:00"""
    return datetime.datetime.combine(dt, datetime.time.min)


def dt_time_max(dt):
    """converts any datetime/date to new datetime with same date and time=23:59:59.999999"""
    return datetime.datetime.combine(dt, datetime.time.max)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python中的时间戳(年、月、日、小时、分钟) - Timestamp in python (year,month,day,hour,minute) 从当前日期时间中提取年、月、日、小时和分钟 - Extracting year, month, day, hour and minute from the current datetime Python Dataframe 将日期时间解析为年、月、日、时、分、秒的列 - Python Dataframe parse datetime into columns for year, month, day, hour, minute, second 如何从日期时间对象中提取小时/分钟并去掉年/月/日/秒部分? - How do I extract the Hour/Minute from a datetime object and get rid of the Year/Month/Day/Second section? 用Python解析年,月,日,时,分,秒 - Parsing year, month, day, hour, minute, second in Python 如何在python中获取当前时间并分解为年、月、日、小时、分钟? - How to get current time in python and break up into year, month, day, hour, minute? 按分钟,小时,天,月和年分组? - groupby minute, hour, day, month, and year? 如何为 Pandas 中的 2 天数据从年、日、小时和分钟列(无月列)创建日期时间 object? - How to create a datetime object from Year, Day, Hour and Minute columns (without month column) for 2 day data in Pandas? Python使用strptime进行解析-月日时:分:秒时区-与格式不匹配 - Python parse with strptime - Month Day Hours:Minute:Seconds Year Timezone - Does not match format 如何从Python日期时间获得时区感知的年,月,日,小时等? - How can I get the timezone-aware year, month, day, hour etc. from a Python datetime?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM