How to find a previous weekday (Sunday, Monday, etc.) from a specific date using Momentjs - momentjs

I'm trying to figure out how to build code that will find the 'MM/DD/YYYY' date of a previous day of the week in relation to a given date, which is 04/10/18.
If I've understood Momentjs' documentation correctly, I think I can use negative weekday numbers to find a past 'X-day' from a specific date.
I know the date I'm using is a Tuesday, so I thought if I gave the weekday function the numerical representation of last Tuesday (-2), I would get the past Tuesday, '04/03/2018'. The problem is that when I run the following check:
expect(await moment('04/10/2018', ['MM/DD/YYYY']).weekday(-2).format('MM/DD/YYYY')).toBe('04/03/2018');
I get a result of '04/06/2018' instead.

The returned value is correct. The function .weekdays(-2) computes two weekdays earlier than the given moment.
In your example, two weekdays before a Tuesday would be a Friday, because one weekday before Tuesday is Monday, and one weekday before that is Friday, so Tuesday 4/10/2018 - 2 weekdays = Friday 4/06/2018.

Related

unexpected behaviour of as.Date() with %W format

I thought I had understood how I can use as.Date() but I get an unexpected result when using the UK convention for counting weeks in a given year (%W).
I have a year-week combination and want to find the date corresponding to Monday and Sunday of that week (if they exist, so it's fine that NA is returned if the week does not contain Mon or Sun). In the UK week counting, the week starts with a Monday. So to find the Monday of, say, week 1 in 2016, I use the following code, which returns the correct result (4 Jan 2016):
as.Date("2016011", format = "%Y%W%u")
To find the Sunday of that week, I change the last number to 7 because %u takes 1 to be Monday and 7 to be Sunday (I have also used %w instead with its definition of Sunday 0 and Monday 1 but with the same result):
as.Date("2016017", format = "%Y%W%u")
My expected output is 10 Jan 2016 but I get 3 Jan 2016. So it seems that as.Date() treats the week as beginning with Sunday. This however contradicts the definition of %W.
Any ideas what I'm missing? Thanks!
For working with dates, I recommend the lubridate package.
Below are some examples.
library(lubridate)
date1 = ymd("20160103")
weekdays(date1) #Local name of the day of the week
#[1] "niedziela"
wday(date1) #Weekday number regional setting
#[1] 1
wday(date1, week_start = 1) #Weekday number where Monday is 1 day
#[1] 7
You can set lubridate.week.start option to control this parameter globally.
Here's a bit more information.

Yearweek is parsed wrongly in R

Problem: I am facing the problem that R parses a date (30 December 2019) into yearweek wrongly (Output: 2019 W01). I do not know why this is happening. Any suggestions what to change/alternative way of coding?
format(lubridate::ymd("2019-12-30"), "%Y W%V")
# Output
# 2019 W01
# Desired Output:
# 2019 W52
From the strptime documentation:
%U
Week of the year as decimal number (00–53) using Sunday as the first day 1 of the
week (and typically with the first Sunday of the year as day 1 of week 1). The US
convention.
%V
Week of the year as decimal number (01–53) as defined in ISO 8601. If the week
(starting on Monday) containing 1 January has four or more days in the new year,
then it is considered week 1. Otherwise, it is the last week of the previous year,
and the next week is week 1. (Accepted but ignored on input.)
%W
Week of the year as decimal number (00–53) using Monday as the first day of week
(and typically with the first Monday of the year as day 1 of week 1). The UK
convention.
It sounds like you may want either %U or %W, depending on whether you want to treat Sunday or Monday as the start of the week.
Note however that these can result in values between 00 and 53, which is a consequence of fixing the start of the week to a particular weekday (either Sunday or Monday). Doing that means that there can actually be a partial week at the start and at the end of the year.
If you prefer to count based on week number 1 beginning on the first day of the year, you can use the function lubridate::week.
For example:
library(lubridate)
year_week <- function(date) paste0(year(date), ' W', week(date))
year_week(ymd("2019-01-01"))
# Result: "2019 W1"
year_week(ymd("2019-12-30"))
# Result: "2019 W52"
After some more research I found that this is the best solution:
format(lubridate::ymd("2019-12-30"), "%G W%V")
Use %G instead of %Y to reflect that the week-based year (%G and %g) may differ from the calendar year (%Y and %y).
See also: https://community.rstudio.com/t/converting-week-number-and-year-into-date/27202/2

What date format is 636529536000000000?

I have to maintain an ASPX page that increments the date/time by passing a value in the querystring in this format:
636529536000000000 in reference to 31 January 2018
636530400000000000 in reference to 01 February 2018
The url format is: /reservas.aspx?t=636530400000000000
What is this date/time format?
It is the number of ticks where a tick is one hundred nanoseconds or one ten-millionth of a second. The number of ticks is measured since the epoch DateTime.MinValue (12:00:00 midnight, January 1, 0001). For example:
new DateTime(636529536000000000).ToString("F", CultureInfo.InvariantCulture)
outputs:
Wednesday, 31 January 2018 00:00:00
Could be a number of days from certain date, similar to julian date calculation:
https://en.wikipedia.org/wiki/Julian_day#Julian_date_calculation
Potentially incorporating the time as well?
Without details of the code I cant really advise from a provided value.

show only weekday from date

I'm trying to use a date value I have in my table to show the start date of a holiday as the day value (Monday, Tuesday etc) how would be the best way to go about getting the start day value in this format in VB.NET?
I'm currently working with the date format dd/mm/yy.
Use Weekday and WeekdayName functions
WeekdayName(Weekday(startDate))
Weekday , which returns a number that indicates the day of the week of a particular date. It considers the ordinal value of the first day of the week to be one.
WeekdayName , which returns the name of the week in the current culture that corresponds to a particular weekday number.

Moment.js issue if trying to set date on 1.1. for actual year

I'm trying to get date for first day of the new year, it means that I tried something like this:
dateFrom = moment().month(0).day(01).format('YYYY-MM-DD');
But it gives me date:
2013-12-30
Instead of the
2014-01-01
How can I solve it please?
Thanks for any advice.
You are looking for date instead of day if you want to define the day of the month.
This works:
moment().month(0).date(1).format('YYYY-MM-DD');
The date method defines day of the month, docs here.
The day method defines the day of the week. From the docs:
So, by using day(1) you are asking to get the nearest Monday. In your case the nearest Monday to January 1st, 2014 is December 30th, 2013

Resources