-1

i have concatenated

date(date field)
time(float)
date_time_to(Char)

i am getting output in this format "2018-10-09 20.00".

I need output in this format '%m/%d/%Y %H:%M:%S'

I used "obj.date_time_to = datetime.strptime(self.date_time_to, "%m/%d/%Y %H:%M:%S.%f")"

i am getting

ValueError: time data '2018-10-09 20.00' does not match format '%m/%d/%Y %H:%M:%S.%f'
iam supreeth
  • 497
  • 4
  • 16
  • 1
    Your format string has to match the input value. Yours uses different dividers and is longer. The order of the date fields is also wrong. – Klaus D. Oct 09 '18 at 04:54
  • 1
    Why exactly are you expecting just `%m/%d/%Y` to match `2018-10-09` ? – OneCricketeer Oct 09 '18 at 04:56
  • 1
    You might want to look at how datetime operates, it seems you do not understand how the formatting parameters work. `%m/%d%Y` would be a date in month/day/Year. You need to adjust it to your input, as seen in @schwobaseggi's answer. – Bernhard Oct 09 '18 at 05:02

2 Answers2

1

You need to parse the string with the right format:

from datetime import datetime

input = "2018-10-09 20.00"
#        ^^^^^^^^^^^^^^^^-----⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄
d = datetime.strptime(input, "%Y-%m-%d %H.%M")  # parse to datetime object
result = d.strftime("%m/%d/%Y %H:%M:%S")        # now convert to desired format
#⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄-^^^^^^^^^^^^^^^^^ 
'10/09/2018 20:00:00'
user2390182
  • 72,016
  • 6
  • 67
  • 89
1

You can use the datetime module. Check the Python documentation.

For example:

import datetime

x = "09/10/2018"
date = datetime.datetime.strptime(date, "%d/%M/%Y") # string to datetime
hearot
  • 124
  • 9