Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove timestamp from date string in Python

I have situation where I should ignore the timestamp in a date string. I have tried the below command but with no luck.

"start" variable used below is in AbsTime (Ex: 01MAY2017 11:45) and not a string.

start_date = datetime.datetime.strptime(start, '%d%^b%Y').date()
print start_date

My output should be :

01MAY2017 or 01MAY2017 00:00

Could any one help me.

like image 385
srisriv Avatar asked Feb 15 '26 06:02

srisriv


1 Answers

Your directives are slightly off, and you need to capture all the contents (irrespective of what you want to retain).

start_date = datetime.datetime.strptime(start, '%d%b%Y %H:%M').date()
print start_date.strftime('%d%b%Y')
# '01May2017'

Update - Adding complete code below:

import datetime
start = '01MAY2017 11:45'
start_date = datetime.datetime.strptime(start, '%d%b%Y %H:%M').date()
print start_date.strftime('%d%b%Y')
# 01May2017
like image 165
shad0w_wa1k3r Avatar answered Feb 16 '26 22:02

shad0w_wa1k3r



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!