Easiest way to combine date and time strings to single datetime object using Python

后端 未结 4 1059
北恋
北恋 2020-12-13 15:59

I have a web form which has 2 input fields, \"StartDate\" and \"StartTime\". I convert the StartDate field\'s string into a Python datetime object

4条回答
  •  忘掉有多难
    2020-12-13 16:22

    If you can load the time into a datetime.time, you can use the following code

    import datetime
    
    dt = datetime.datetime(2012, 2, 12)
    tm = datetime.time(1, 30)
    
    combined = dt.combine(dt, tm)
    
    print(combined)
    

    Output

    2012-02-12 01:30:00
    

提交回复
热议问题