组合日期时间(带有时间部分)和时间对象 [英] Combining datetime (with time component) and time objects

查看:86
本文介绍了组合日期时间(带有时间部分)和时间对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有日期时间对象,其信息如 2013-10-01 14:15:00 和时间对象,如


  1. 1900-01-01 00:00:49.235000

  2. 1900-01-01 00:00:49.465000

  3. 1900-01-01 00:00:49.695000

时间对象实际上是渐进增量,我想添加到日期时间对象中。如此处

$ b所述
$ b


如果date是datetime对象,则其时间分量和tzinfo属性将被忽略。


我写了下面的代码。

  def CombineDateTime(date_str,time_str),应该如何更新才能将这些时间对象添加到datetime对象中? ,date_fmt,time_fmt,设备,sample_name):

导入日期时间

尝试:
date_obj = datetime.datetime.strptime(date_str,date_fmt)

time_obj = datetime.datetime.strptime(time_str,time_fmt)

返回datetime.datetime.combine(date_obj.date(),time_obj.time())

,除了:
返回错误

输出:


  1. 2013-10-01 00:00:49.235000

  2. 2013-10-01 00:00:49.465000

  3. 2013-10-01 00:00:49.695000

预期输出:


  1. 2013-10-01 14:15:49.235000

  2. 2013-10-01 14:15:49.465000

  3. 2013-10-01 14 :15:49.695000


  4. 解决方案

    您得到的输出是因为 2013-10-01 00:00:49.235000 到您的datetime.datetime.combine()以及date_obj 14:15:00 中显示的时间



    因此,您可以在合并之前将date_obj timedelta添加到time_obj中。

      date_time = date_obj.time()
    time_obj + = datetime.timedelta(hours = date_time.hour,minutes = date_time.minute,seconds = date_time.second, microseconds = date_time.microsecond)

    也就是说,

      def CombineDateTime(date_str,time_str,date_fmt,time_fmt,设备,样品名称):
    导入日期时间
    尝试:
    date_obj = datetime.datetime。 strptime(date_str,date_fmt)
    date_time = date_obj.time()

    time_obj = datetime.datetime.strptime(time_str,time_fmt)
    time_obj + = datetime.timedelta( hours = date_time.hour,minutes = date_time.minute,seconds = date_time.second,microseconds = date_time.microsecond)

    返回datetime.datetime.combine(date_obj.date(),time_obj.time() )

    例外,例如e:
    #print e
    返回'错误'


    Well, I have datetime objects with information like 2013-10-01 14:15:00 and time objects like

    1. 1900-01-01 00:00:49.235000
    2. 1900-01-01 00:00:49.465000
    3. 1900-01-01 00:00:49.695000

    The time objects are actually progressive increments which I'd like to add to the datetime object. As described here,

    If date is a datetime object, its time components and tzinfo attributes are ignored.

    I wrote the code below. How should it be updated to be able to add these time objects to the datetime object as increments?

    def CombineDateTime(date_str, time_str, date_fmt, time_fmt, equipment, sample_name):
    
    import datetime
    
    try:
        date_obj = datetime.datetime.strptime(date_str, date_fmt)
    
        time_obj = datetime.datetime.strptime(time_str, time_fmt)
    
        return datetime.datetime.combine(date_obj.date(), time_obj.time())
    
    except:
        return 'Error'
    

    Output:

    1. 2013-10-01 00:00:49.235000
    2. 2013-10-01 00:00:49.465000
    3. 2013-10-01 00:00:49.695000

    Expected output:

    1. 2013-10-01 14:15:49.235000
    2. 2013-10-01 14:15:49.465000
    3. 2013-10-01 14:15:49.695000

    解决方案

    The output that you get is because 2013-10-01 and 00:00:49.235000 to your datetime.datetime.combine() and the time present in date_obj 14:15:00 is not taken into consideration at that point!

    Therefore, you have the add the date_obj timedelta to your time_obj before you combine!

    date_time=date_obj.time()
    time_obj += datetime.timedelta(hours=date_time.hour,minutes=date_time.minute,seconds=date_time.second,microseconds=date_time.microsecond)
    

    That is,

    def CombineDateTime(date_str, time_str, date_fmt, time_fmt, equipment, sample_name):
        import datetime
        try:
            date_obj = datetime.datetime.strptime(date_str, date_fmt)
            date_time=date_obj.time()
    
            time_obj = datetime.datetime.strptime(time_str, time_fmt)
            time_obj += datetime.timedelta(hours=date_time.hour,minutes=date_time.minute,seconds=date_time.second,microseconds=date_time.microsecond)
    
            return datetime.datetime.combine(date_obj.date(),time_obj.time())
    
        except Exception as e:
            #print e 
            return 'Error'
    

    这篇关于组合日期时间(带有时间部分)和时间对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆