3

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.

10
  • 1
    start.split()[0] will give you the first output Commented Apr 10, 2017 at 6:59
  • 1
    Curious: where did you get the %^b idea from? Commented Apr 10, 2017 at 7:07
  • @MartijnPieters Might be a typo. % & ^ are on 5 & 6 (shift) on the regular keyboard. Commented Apr 10, 2017 at 7:09
  • @AshishNitinPatil: ah, yes, that's a plausible explanation. Commented Apr 10, 2017 at 7:10
  • I want the Month in Upper case, that's the reason i used %^b. Commented Apr 10, 2017 at 7:15

4 Answers 4

5

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
Sign up to request clarification or add additional context in comments.

7 Comments

I have tried the same, but getting the below error: AttributeError: type object 'datetime.datetime' has no attribute 'datetime'
try import datetime instead of from datetime import datetime
Use the code in the latest answer. You might have imported datetime twice in the interpreter.
"TypeError: strptime() argument 1 must be string, not AbsTime"
@srisriv: please update your question with a proper minimal reproducible example then that includes more detail about your actual objects. If you are not using str values (which your question implies), it is your job to tell us what you do have.
|
0

https://docs.python.org/2/library/datetime.html

To convert the string back to a datetime we can use strptime()

Example using strptime()

datetime.datetime.strptime('10Apr2017 00:00', '%d%b%Y %H:%M')

In [17]: datetime.datetime.strptime('10Apr2017 00:00', '%d%b%Y %H:%M')
Out[17]: datetime.datetime(2017, 4, 10, 0, 0)

Use strftime to form your datetime object

Example using now() returns an datetime object that we form to a string

datetime.datetime.now().strftime('%d%b%Y')

In [14]: datetime.datetime.now().strftime('%d%b%Y')
Out[14]: '10Apr2017'

6 Comments

The OP wants to retain only the day.
... and not the current day.
that was just an example. Important part strftime
That is incorrect too. It will give ValueError: unconverted data remains: 11:45.
My bad, you are using now() and not OP's format, so this would work.
|
0

try this

  import datetime 
  start = '01MAY2017 11:45' 
  start_date = datetime.datetime.strptime(start, '%d%b%Y %H:%M')
  print start_date.strftime('%Y-%m-%d')

Comments

0

Actually My Best Option to use the very same datetime object to subtract hours , if you do not wish to use strings and use the datetime Object itself.

Here's an Example:

In [1]: date.strftime('%c')
Out[2]: 'Wed Jul 10 00:00:00 2019'

In [3]: date = datetime.datetime.utcnow()

In [4]: date.strftime('%c')
Out[5]: 'Wed Jul 10 19:06:22 2019'

In [6]: date = date - datetime.timedelta(hours = date.hour, minutes = date.minute, seconds = date.second) #Removing hours, mins,secs

In [7]: date.strftime('%c') #use 
Out[8]: 'Wed Jul 10 00:00:00 2019'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.