Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain the day of the week in a 3 letter format from a datetime object in python?

Tags:

python

I am working on a script were I must work with datetime objects in Python. At some point I have one of those objects and I need to get the day of the week (which is a number value) in a 3-letter format (i.e. Tue, Wed, etc.). Here is a brief sample of the code, in dateMatch.group() all I am doing is getting pieces of a string obtained via regex matching.

from datetime import datetime

day = dateMatch.group(2)
month = dateMatch.group(3)
year = dateMatch.group(4)
hour = dateMatch.group(5)
minute = dateMatch.group(6)
second = dateMatch.group(7)

tweetDate = datetime(int(year), months[month], int(day), int(hour), int(minute), int(second))

From that date time object I get a numerical day value (i.e. 18) and I need to convert it to (i.e. Tue).

Thanks!

like image 398
Oscar_Mariani Avatar asked Mar 19 '13 20:03

Oscar_Mariani


1 Answers

http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

date, datetime, and time objects all support a strftime(format) method, to create a string representing the time under the control of an explicit format string.

...

%a — Locale’s abbreviated weekday name.

>>> datetime.datetime.now().strftime('%a')
   'Wed'
like image 178
Pavel Anossov Avatar answered Sep 21 '22 03:09

Pavel Anossov