-5

I'm receiving data from the port.

This data has date and time.But not formatted.

Example:

'02012019', '090253'

I want to save it to the database and I want to convert this format.

02-01-2019   09:02:53

Is there a function for this? Or is it a problem if I write? It will run 200 times in 5 seconds.

Anybody have a suggestion?

Tota1907
  • 23
  • 2
  • 7
  • All that has been posted is a program description. However, we need you to [ask a question](//stackoverflow.com/help/how-to-ask). We can't be sure what you want from us. Please [edit] your post to include a valid question that we can answer. Reminder: make sure you know [what is on-topic here](//stackoverflow.com/help/on-topic); asking us **to write the program** for you, suggestions, and external links are off-topic. What have you tried, please show your [mcve] – Patrick Artner Jan 02 '19 at 09:11
  • Integers don't have leading zeros. If you receive characters you can concatenate them in a string and use `datetime.strptime` to parse it. – Panagiotis Kanavos Jan 02 '19 at 09:12
  • 'I want to save it to the database.' - it's probably best to store as datetime in which case the stored format would be 2019-01-02 09:02:53 do you really want it stored in the format you specified? – P.Salmon Jan 02 '19 at 09:12
  • BTW database date types have no format either. They are binary values, like double, float, int. Once you parse the string into a `datetime` object you can store it into a `datetime` typed field in the database – Panagiotis Kanavos Jan 02 '19 at 09:13

1 Answers1

0
from datetime import datetime

date_data  ='02012019 09:02:53' 
data = datetime.strptime(date_data,'%d%m%Y %H:%M:%S')
result = data.strftime('%d-%m-%Y %H:%M:%S')

# output '02-01-2019 09:02:53'
sahasrara62
  • 10,069
  • 3
  • 29
  • 44