Home > Software engineering >  converting float number into datetime format
converting float number into datetime format

Time:12-15

I have a file that contains DateTime in float format
example 14052020175648.000000 I want to convert this into 14-05-2020 and leave the timestamp value.

input ==> 14052020175648.000000

expected output ==> 14-05-2020

CodePudding user response:

Use pd.to_datetime:

df = pd.DataFrame({'Timestamp': ['14052020175648.000000']})

df['Date'] = pd.to_datetime(df['Timestamp'].astype(str).str[:8], format='%d%m%Y')
print(df)

# Output:
               Timestamp       Date
0  14052020175648.000000 2020-05-14

I used astype(str) in case where Timestamp is a float number and not a string, so it's not mandatory if your column already contains strings.

CodePudding user response:

This can solve your problem

from datetime import datetime
string = "14052020175648.000000"
yourDate = datetime.strptime(string[:8], '%d%m%Y').strftime("%d-%m-%Y")
print(yourDate)

Output:

14-05-2020
  • Related