day of the week to day number (Monday = 1, Tuesday = 2)
$day_of_week = date('N', strtotime('Monday'));
Convert a date to a day number (monday = 1, tuesday = 2) in R
You can do this easily with lubridate
although it indexes Sunday as '1'. So since March 1, 2014 was a Saturday it would return '7'
library(lubridate)
wday(mdy("3-1-2014"))
[1] 7
How can I turn a date into the day of the week (as an integer)?
We can convert the column to str
first, then to datetime
and use .dt.weekday
:
df = pd.DataFrame({'date': [20210101, 20210102]})
df['weekday'] = pd.to_datetime(df['date'].astype(str)).dt.weekday
df
Output:
date weekday
0 20210101 4
1 20210102 5
P.S. Here Monday = 0, ..., Sunday = 6, so if you want 1-7 instead, you can add 1 to column values
Getlocalday from Day to number
You need to incorporated our solutions into the code on your own page. Here, I am showing a sample heading that contains a span
that will be updated with the day number.
window.addEventListener("DOMContentLoaded", function(){ // Get a reference to the <span> element on the page var label = document.getElementById("day"); // Inject the answer from the function into it: label.textContent = getLocalDay(new Date()); });
function getLocalDay(date) { var day = date.getDay();
var weekdays = ["Monday","Tuesday" ,"Wednesday" , "Thursday" ,"Friday" ,"Saturday" ,"Sunday" ]; let dayNum = null; weekdays.forEach(function(d, index, arry){
if(d === weekdays[day - 1]){ dayNum = index; } }); return dayNum;}
<h1>The day number for today is: <span id="day"></span></h1>
Which day of the week do we receive more number of orders without counting actual number of order in particular date in excel?
You can get the max number of orders in a single formula if you're willing to make it more complex with a term for each weekday:
=MAX(SUMPRODUCT(--(WEEKDAY(ORDERS!C2:C16799,2)=1)),
SUMPRODUCT(--(WEEKDAY(ORDERS!C2:C16799,2)=2)),
SUMPRODUCT(--(WEEKDAY(ORDERS!C2:C16799,2)=3)),
SUMPRODUCT(--(WEEKDAY(ORDERS!C2:C16799,2)=4)),
SUMPRODUCT(--(WEEKDAY(ORDERS!C2:C16799,2)=5)),
SUMPRODUCT(--(WEEKDAY(ORDERS!C2:C16799,2)=6)),
SUMPRODUCT(--(WEEKDAY(ORDERS!C2:C16799,2)=7)))
To get the weekday, the following might work-- it does not work in my Excel 2016 because the array in the second argument isn't recognized but the array functions have been improved in newer versions, so you could try it.
=MATCH(
MAX(
SUMPRODUCT(--(WEEKDAY(ORDERS!C2:C16799,2)=1)),
SUMPRODUCT(--(WEEKDAY(ORDERS!C2:C16799,2)=2)),
SUMPRODUCT(--(WEEKDAY(ORDERS!C2:C16799,2)=3)),
SUMPRODUCT(--(WEEKDAY(ORDERS!C2:C16799,2)=4)),
SUMPRODUCT(--(WEEKDAY(ORDERS!C2:C16799,2)=5)),
SUMPRODUCT(--(WEEKDAY(ORDERS!C2:C16799,2)=6)),
SUMPRODUCT(--(WEEKDAY(ORDERS!C2:C16799,2)=7))
),
{
SUMPRODUCT(--(WEEKDAY(ORDERS!C2:C16799,2)=1)),
SUMPRODUCT(--(WEEKDAY(ORDERS!C2:C16799,2)=2)),
SUMPRODUCT(--(WEEKDAY(ORDERS!C2:C16799,2)=3)),
SUMPRODUCT(--(WEEKDAY(ORDERS!C2:C16799,2)=4)),
SUMPRODUCT(--(WEEKDAY(ORDERS!C2:C16799,2)=5)),
SUMPRODUCT(--(WEEKDAY(ORDERS!C2:C16799,2)=6)),
SUMPRODUCT(--(WEEKDAY(ORDERS!C2:C16799,2)=7))
},
0)
The first part just determines the max value; the second argument of the MATCH
function is an array of each of the values by weekday, the MATCH function returns the index of the matching value.
Mysql WEEKDAY() vs Mysql DayofWeek()
A relevant difference is that weekday counts the days of the week from Monday, as follows and start from 0
0=Monday, 1=Tuesday, 2=Wednesday, 3=Thursday, 4=Friday, 5=Saturday, 6=Sunday
On the other hand dayofweek counts the days of the week from Sunday and start with 1
1=Sunday, 2=Monday, 3=Tuesday, 4=Wednesday, 5=Thursday, 6=Friday, 7=Saturday
Please try
select weekday(now()) weekday, dayofweek(now()) dayofweek;
Related Topics
How to Decode a JSON String with Several Objects in PHP
When to Wrap Curly Braces Around a Variable
Interview Question: in PHP, Is 123==0123
How to Convert Between Time Zones in PHP Using the Datetime Class
PHP Jquery Ajax Call Throws Net::Err_Empty_Response
.Htaccess Redirect Non-Www to Www Preserving Uri String
How to Loop Through JSON Array
Converting HTML Table to a CSV Automatically Using PHP
Bulk Rename Files in a Folder - PHP
How to Get Random Image from Directory Using PHP
Laravel 5 Socialite - Curl Error 77: Error Setting Certificate Verify Locations
Error Checking for Pdo Prepared Statements
How to Swap Colors in Image Using Gd Library in PHP