Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Date String to Day of Week

I have date strings like this:

'January 11, 2010' 

and I need a function that returns the day of the week, like

'mon', or 'monday' 

etc. I can't find this anywhere in the Python help. Anyone? Thanks.

like image 707
zbinsd Avatar asked May 27 '13 05:05

zbinsd


People also ask

How do you find the day of the week from a date string?

use date. weekday() Return the day of the week as an integer, where Monday is 0 and Sunday is 6.

How do you convert a date to a day of the week in Python?

isoweekday() to get a weekday of a given date in Python Use the isoweekday() method to get the day of the week as an integer, where Monday is 1 and Sunday is 7. i.e., To start from the weekday number from 1, we can use isoweekday() in place of weekday() . The output is 1, which is equivalent to Monday as Monday is 1.

How do you convert a date into a day of the week in R?

Method 2: Using strftime() function. In this method of converting the date of the week, the user needs to call strftime() function which is an in-built function and pass the respected parameters into it, and then in return, the function will give the weekdays of the given date to the user.


2 Answers

You might want to use strptime and strftime methods from datetime:

>>> import datetime >>> datetime.datetime.strptime('January 11, 2010', '%B %d, %Y').strftime('%A') 'Monday' 

or for 'Mon':

>>> datetime.datetime.strptime('January 11, 2010', '%B %d, %Y').strftime('%a') 'Mon' 
like image 73
Andrey Sobolev Avatar answered Sep 22 '22 02:09

Andrey Sobolev


use date.weekday() Return the day of the week as an integer, where Monday is 0 and Sunday is 6.

http://docs.python.org/2/library/datetime.html#datetime.date.weekday

like image 23
Lahiruzz Avatar answered Sep 24 '22 02:09

Lahiruzz