Jonas Kruse
Jonas Kruse

Reputation: 11

Re-format datetime

I'm trying to change the datetime format from "2019-02-08 15:00" to "8 feb", but the hour and minute seems to stop me from doing this in an easy way.

The variable containing this info is my_task['due']['string'] and if I print it I get "2019-02-08 15:00".

Then I run this code:

variabel = time.strftime('%d %b', my_task['due']['string'])

But I am getting the error:

TypeError: argument must be sequence of length 9, not 16

In PHP, which I'm quite good at, this is easy peasy, but I'm guessing I'm missing some small detail here. Can someone please give me a push in the right direction?

Upvotes: 1

Views: 46

Answers (1)

benvc
benvc

Reputation: 15130

Appears that you are working with datetime strings and not datetime objects, so you will first need to create a datetime object from your string in order to use datetime.strftime() to reformat it.

Also note that datetime objects and time objects are not the same despite sharing some common methods.

For example:

from datetime import datetime

formatted = datetime.strptime('2019-02-08 15:00', '%Y-%m-%d %H:%M').strftime('%d %b')
print(formatted)
# OUTPUT
# 08 Feb

Upvotes: 2

Related Questions