Convert Week Number to Date

Convert week number to date

as.Date is calling the 1 to 9 as NA as it is expects two digits for the week number and can't properly parse it.

To fix it, add in some - to split things up:

as.Date(paste(2014, df$Week, 1, sep="-"), "%Y-%U-%u")

Convert Week number to date in data frame

You can do this in base R using as.Date itself.

Based on your attempt it seems your locale is English, so you can try :

as.Date(paste(df$Week, df$Year, 'Sun'), '%U %Y %a')
#[1] "2019-01-06" "2019-01-13" "2020-01-05" "2020-01-05"

Excel how to convert weeknum to date when crossing the year

You can do this by taking the weekday number of the date away from itself and adding 7-weekday to itself.

i.e.

If we use the first date on your list and we say weeks start on Monday:

The first day of the week (Monday) = E2-WEEKDAY(E2,2)+1 = 12/27/21

The last day of the week (Sunday) = E2+(7-WEEKDAY(E2,2)) = 01/02/22

Note: I have assumed your first date (12/27/2021) is in Cell E2

If you want Sunday to be the first day of the week, simply add a 1 instead of a 2 into the WEEKDAY part of the formula.

Convert week number and year to a date in Access-SQL?

This is not so simple, as the ISO years rarely are in sync with calendar years.

But this function will do:

' Returns the date of Monday for the ISO 8601 week of IsoYear and Week.
' Optionally, returns the date of any other weekday of that week.
'
' 2017-05-03. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function DateYearWeek( _
ByVal IsoWeek As Integer, _
Optional ByVal IsoYear As Integer, _
Optional ByVal DayOfWeek As VbDayOfWeek = VbDayOfWeek.vbMonday) _
As Date

Dim WeekDate As Date
Dim ResultDate As Date

If IsoYear = 0 Then
IsoYear = Year(Date)
End If

' Validate parameters.
If Not IsWeekday(DayOfWeek) Then
' Don't accept invalid values for DayOfWeek.
Err.Raise DtError.dtInvalidProcedureCallOrArgument
Exit Function
End If
If Not IsWeek(IsoWeek, IsoYear) Then
' A valid week number must be passed.
Err.Raise DtError.dtInvalidProcedureCallOrArgument
Exit Function
End If

WeekDate = DateAdd(IntervalSetting(dtWeek), IsoWeek - 1, DateFirstWeekYear(IsoYear))
ResultDate = DateThisWeekPrimo(WeekDate, DayOfWeek)

DateYearWeek = ResultDate

End Function

However, it uses a series of supporting functions, like:

' Returns the primo date of the week of the date passed.
'
' 2016-01-13. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function DateThisWeekPrimo( _
ByVal DateThisWeek As Date, _
Optional ByVal FirstDayOfWeek As VbDayOfWeek = vbSunday) _
As Date

Dim Interval As String
Dim Number As Double
Dim ResultDate As Date

Number = 0
Interval = IntervalSetting(DtInterval.dtWeek)

ResultDate = DateIntervalPrimo(Interval, Number, DateThisWeek, FirstDayOfWeek)

DateThisWeekPrimo = ResultDate

End Function

and several more - way too much to post here.

So, please refer to my project at GitHub, VBA.Date, for the modules holding the full code.

Convert week number to week (date) with Pandas

Check

pd.to_datetime(my_net_diet_raw['Date & Time']).dt.to_period('w').astype(str).str.split('/').str[0]

How do I convert a calendar week into a date in Excel?

For ISO week numbers you can use this formula to get the Monday

=DATE(A2,1,-2)-WEEKDAY(DATE(A2,1,3))+B2*7

assuming year in A2 and week number in B2

it's the same as my answer here https://stackoverflow.com/a/10855872/1124287

Convert week number in dataframe to start date of week (Monday)

Try using pd.to_datetime on the 'Year' and 'Week_Number' columns with a format string for Year, Week of Year, and Day of Week ('%G%V%w'):

df = df.reset_index()
df['week_date'] = pd.to_datetime(
df['Year'].astype(str) + df['Week_Number'].astype(str) + "1",
format='%G%V%w'
)
df = df.set_index(['Year', 'Week_Number'])

The + "1" is for the day of the week. Week numbers 0-6 with 0 being Sunday and 6 being Saturday. (Ref. Format Codes)

df:

                          open         close  week_date
Year Week_Number
2020 31 11106.793367 11059.660924 2020-07-27
32 11059.658520 11653.660942 2020-08-03

UNIX date: How to convert week number (date +%W) to a date range (Mon-Sun)?

With GNU date:

$ cat weekof.sh
function weekof()
{
local week=$1 year=$2
local week_num_of_Jan_1 week_day_of_Jan_1
local first_Mon
local date_fmt="+%a %b %d %Y"
local mon sun

week_num_of_Jan_1=$(date -d $year-01-01 +%W)
week_day_of_Jan_1=$(date -d $year-01-01 +%u)

if ((week_num_of_Jan_1)); then
first_Mon=$year-01-01
else
first_Mon=$year-01-$((01 + (7 - week_day_of_Jan_1 + 1) ))
fi

mon=$(date -d "$first_Mon +$((week - 1)) week" "$date_fmt")
sun=$(date -d "$first_Mon +$((week - 1)) week + 6 day" "$date_fmt")
echo "\"$mon\" - \"$sun\""
}

weekof $1 $2
$ bash weekof.sh 12 2012
"Mon Mar 19 2012" - "Sun Mar 25 2012"
$ bash weekof.sh 1 2018
"Mon Jan 01 2018" - "Sun Jan 07 2018"
$


NOTE:

As the OP mentions, the week number is got by date +%W. According to GNU date's manual:

%W: week number of year, with Monday as first day of week (00..53)

So:

  1. Each week starts from Mon.
  2. If Jan 1 is Mon, then the first week will be week #1.
  3. If Jan 1 is not Mon, then the first few days will be week #0 and the week #1 starts from the first Mon.

How to convert week numbers into date format using R

Maybe there is a more automated way, but try something like this. I think this gets the right days, I looked at a 2020 calendar and counted. But if something is off, its a matter of playing with the (week - 1) * 7 - 1 component to return what you want.

This just grabs the first day of the year, adds x weeks worth of days, and then uses ceiling_date() to find the next Sunday.

library(dplyr)
library(tidyr)
library(lubridate)

df %>%
separate(week, c("year", "week"), sep = 4, convert = TRUE) %>%
mutate(date = ceiling_date(ymd(paste(year, "01", "01", sep = "-")) +
(week - 1) * 7 - 1, "week", week_start = 7))

# # A tibble: 6 x 4
# year week Revenue date
# <int> <int> <dbl> <date>
# 1 2020 9 4543 2020-03-01
# 2 2020 10 6764 2020-03-08
# 3 2020 11 2324 2020-03-15
# 4 2020 12 5674 2020-03-22
# 5 2020 13 2232 2020-03-29
# 6 2020 14 2323 2020-04-05


Related Topics



Leave a reply



Submit