How do I get the day of week given a date?
Use weekday()
:
>>> import datetime
>>> datetime.datetime.today()
datetime.datetime(2012, 3, 23, 23, 24, 55, 173504)
>>> datetime.datetime.today().weekday()
4
From the documentation:
Return the day of the week as an integer, where Monday is 0 and Sunday is 6.
Find the day of a week
df = data.frame(date=c("2012-02-01", "2012-02-01", "2012-02-02"))
df$day <- weekdays(as.Date(df$date))
df
## date day
## 1 2012-02-01 Wednesday
## 2 2012-02-01 Wednesday
## 3 2012-02-02 Thursday
Edit: Just to show another way...
The wday
component of a POSIXlt
object is the numeric weekday (0-6 starting on Sunday).
as.POSIXlt(df$date)$wday
## [1] 3 3 4
which you could use to subset a character vector of weekday names
c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday")[as.POSIXlt(df$date)$wday + 1]
## [1] "Wednesday" "Wednesday" "Thursday"
Is there an algorithm for finding a day of a week (Sunday, Monday, etc.)?
Is there an algorithm for finding a day of a week ... with a date (day and month, no year) while using only the starting day of the year ... **
Yes for first 2 months. Hint: begin with (starting_day + 31*(Month==Feb) + day)%7
.
No, for days March 1st or later. Code needs to know if the year is a leap year.
How to find day of the week for each date of that month if the day of first date is given?
I see a couple of problems in your code. From the way you are calling returnDay(date), it looks like you want to have the date as a parameter to this function. You cannot retrieve the date as input from the user, from within the same function that also needs the date as an argument. So first rewrite your code so that you set the input from outside this function scope, and then supply it to your function for processing. The same goes for the integer part of your date. Now for the logic behind calculating the day to return:
- A user supplies the first day
- A user supplies the current date
let's go
def returnDay(firstDay,currentDate):
days_in_week = ['Monday','Tuesday', 'Wednesday', 'Thursday', 'Friday',
'Saturday', 'Sunday']
day_offset = 0
for i in range(7):
if firstDay == days_in_week[i]:
day_offset += i
index_days_in_week = (currentDate + day_offset)%7 - 1
return days_in_week[index_days_in_week]
def main():
firstDay = input("supply the first day of the month")
currentDate = input("supply current day of the month")
returnDay(firstDay, currentDate)
main()
You may need to optimise it in order to work completely. I leave this as a homework assessment to you.
How can I get the day of the week with date given?
There were quite a few things that were wrong with your code.
First is that you were incorrectly using a return statement. Any code after your return statement in the dayOfWeek
function won't be run.
Also, var date1 = 11/19/2001
is 100% not what you think it is. As it is now, it's two division operations, and not a string or date object. Notice in my code there are quotes surrounding the date value.
function dayOfWeek (date) {
const days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
return days[date.getDay()];
}
var date1 = new Date('11/19/2001');
console.log(dayOfWeek(date1));
var date2 = new Date('12/19/1969');
console.log(dayOfWeek(date2));
How do I find the date of the first day of week given any date and assuming Monday as day 1 in Excel
Try
=GivenDate-WEEKDAY(GivenDate,2)+1
replace GivenDate
with a valid date or the cell reference of a valid date, such as 20/10/2019
which shall return 14/10/2019
.
Related Topics
How to Trim Leading and Trailing White Space
Relative Frequencies/Proportions With Dplyr
Plot Two Graphs in Same Plot in R
How to Use R'S Ellipsis Feature When Writing Your Own Function
How to Create a Lag Variable Within Each Group
Drop Data Frame Columns by Name
Convert a List to a Data Frame
Finding Local Maxima and Minima
Add Column Which Contains Binned Values of a Numeric Column
Convert Continuous Numeric Values to Discrete Categories Defined by Intervals
Counting Unique/Distinct Values by Group in a Data Frame
Dictionary Style Replace Multiple Items
I Want to Split Street Address into Two Columns. One With Street Number Other With Street Name
Gather Multiple Sets of Columns
Split an Audio File into Pieces of an Arbitrary Size
Replace a Value in a Data Frame Based on a Conditional ('If') Statement