0

I Have a value of

datetime.datetime(2015, 1, 28, 13, 53, 36)

and I want it to be printed as string as

Wednesday, January 28, 2015 13:53:36

I tried to

import datetime x = datetime.datetime(2015, 1, 28, 13, 53, 36) z = strptime(str(x), '%A, %B %d, %Y %H:%M:%S')

but got:

Traceback (most recent call last): File "<pyshell#40>", line 1, in <module> z = strptime(str(x), '%A, %B %d, %Y %H:%M:%S') NameError: name 'strptime' is not defined

does anyone have an idea about this?

0

2 Answers 2

3

Given your import - the correct form of accessing strptime is datetime.datetime.strptime (parsing a string to a date) - but it doesn't look like you want that, instead you're after datetime.datetime.strftime (formats a date to a string).

Ultimately though, Python's datetime object has custom string formatting which can be accessed via str.format and will save a bit of typing, eg:

>>> dt = datetime.datetime(2015, 1, 28, 13, 53, 36)
>>> format(dt, '%A, %B %d, %Y %H:%M:%S')
'Wednesday, January 28, 2015 13:53:36'
Sign up to request clarification or add additional context in comments.

Comments

-1

Use the date method:

datetime.datetime.date()

You should use datetime.datetime.strptime:

import datetime

dt = datetime.datetime.strptime(string_date, fmt)

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.