Moment.js first day of the week incorrect - momentjs

I'm working with Moment.js for my first time. I tried to retrieve the date of the first day of the week, and in Europe this is normally Monday. Whatever I do, I get Sunday as result of the first day of the week.
I tried to set-up different languages (local or globally), but to no avail. I use the langs.min.js file from the moment.js github page. The language file of "en-gb" and "fr" has the line of code:
dow : 1, // Monday is the first day of the week.
So I would get the date of monday when I ask for the first day of the week right? I keep getting Sunday as output.
// Create moment object
var localLang = moment();
// Set language to french
localLang.lang('fr');
// Test language
localLang.lang(); // Output: fr
// Retrieve first day of the week and format it
var dow = localLang.startOf('week').format('dddd DD-MM-YYYY'); // Output: dimanche 14-04-2013
Dimanche is french for Sunday.. As you see, moment.js can use the language file succesfully but doesn't use the day of the week configuration
JSfiddle with moment.js and langs.js to test:
JSFiddle
edit:
I can get the date of Monday instead of Sunday with day(1) instead of startOf('week'). But using day(0)I still get Sunday as a result. Why isn't monday the first day of the week, as configured in the language files.

For anyone who come across this question lately, moment now support lang method to set locale specific setting.
Setting first week to be Monday:
moment.lang('zh-cn', {
week : {
dow : 1 // Monday is the first day of the week
}
});
var date = moment().weekday(0); // date now is the first day of the week, (i.e., Monday)

We are working on adding locale aware weekdays, but it hasn't been finished yet.
https://github.com/timrwood/moment/issues/613#issuecomment-13786429

Related

How to get first date of the week based on week number and year in Moment.js?

I tried to get the start date of a specific week in Moment.js from the week number and year by doing moment().year(...).isoWeek(...).startOf('isoWeek')
But It seems that this function is not always returning the correct date.
For example when I live in England and a week always starts on a monday.
We should get 31 Dec 2018 when we ask for the first day of week 1, 2019.
This wasn't the case on 31 Dec 2018 as the result I received was 30 Dec 2019 as the begin date of week 1, 2019. See example
I think I found the solution I was looking for
moment()
.isoWeekYear(year)
.isoWeek(week)
.startOf('week')
Please note that, as i18n section of the docs states:
By default, Moment.js comes with English (United States) locale strings. If you need other locales, you can load them into Moment.js for later use.
So if you want to use en-gb locale you have explicitly load it (in the browser, you can use en-gb.js file or moment-with-locales.js and then set locale using moment.locale('en-gb')).
You don't have to use year() setter, because it sets the year to 2019 and moment().year(2019).isoWeek(1) gives you the first isoweek of the 2020. You can create a moment object for a given year using moment({y: year}) instead.
You have to use week() instead of isoWeek if you want locale dependent results:
Because different locales define week of year numbering differently, Moment.js added moment#week to get/set the localized week of the year.
The week of the year varies depending on which day is the first day of the week (Sunday, Monday, etc), and which week is the first week of the year.
Here a full code sample:
// Set locale to British English
moment.locale('en-gb');
var year = 2019;
var firstMonday = moment({y: year}) // get first day of the given year
.week(1) // get the first week according locale
.startOf('week'); // get the first day of the week according locale
// Show result
console.log(firstMonday.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment-with-locales.min.js"></script>
You can use format() to display the value of a moment object.

How to get the exact week number if the week is starting with specified weekday on moment js?

Example:
Week day is starting from
moment().weekYear(yearValue).week(weekNumber).startOf('week').weekday(2)
which is Tuesday.
First week on 2017 the days are,
[3-Jan-2017, 4-Jan-2017, 5-Jan-2017, 6-Jan-2017, 7-Jan-2017, 8-Jan-2017, 9-Jan-2017]
How to get the exact week number for 1-Jan-2017?
Week numbers can be retrieved by:
var weeknumber = moment("1-Jan-2017", "d-MMM-YYYY", "en").week();
And be careful, passing the locale might change the week number because of starting on Monday or Dunday.
var weeknumber = moment("1-Jan-2017").week();
In my application I've served the locale globally depending on what language the user has logged in with, so I wouldn't worry about that.

momentjs get date by providing week and weekday

I there a way I can get a date through week and weekday?
so far I have for example:
var week_number= 42, weekday = 4;
I have read most of the docs and searches in google and found nothing regarding this.
NOTE: To keep my code standard I would like to use momentjs
UPDATE: I am able to get the date through the week but what about the real date through weekday number?
var week_date = moment().week(week_number),
weekday_date = ????
You probably want to do this:
moment().week(42).weekday(4).startOf('day')
Though you should consider whether you should use week or isoWeek, and you should also consider whether to use weekday, day, or isoWeekday.
All of these functions, and the differences between them are in the moment.js documentation.

Format for vcalendar for weeks and day of week

I generate a vcalendar file with DTSTART and DTEND with week numbers and day of week just like RFC5545 and ISO.8601.2004 1 says. Like so:
DTSTART:2015W437T200000Z
DTEND:2015W437T210000Z
or
DTSTART:2015-W43-7T200000Z
DTEND:2015-W43-7T210000Z
Which read year 2015 week 43 day 7 (Sunday) 8.00pm to 9.00pm.
But neither google-calendar nor an online validator I've come across says that it's correct. Anyone got an idea whats going on?
As the Calendar API docs state, the format does not take weeks; instead, it uses RFC3339 and should read
DTSTART:2015-10-25T20:00:00Z
DTEND:2015-10-25T21:00:00Z

Reporting Services Expression for Week of Year

I have a Report that I send a parameter to as 'WeekStart'. This is based on a selection a user makes on a datepicker.
I'm using the following to extract the week of the year:
=DatePart("ww", Parameters!WeekStart.Value)
The problem I'm having is that when I pick the day 03/01/2012 (dd/MM/yyyy format), the week of the year is returned as 9, which would technically be true had the date been 03/01/2012 with a dateformat of MM/dd/yyyy.
I've tried using CDate, FormatDateString etc but nothing seems to be working. I either get #Error or it returns as the 9th week of the year.
Any suggestions?
What you can do is this:
use YourDatabase
go
set dateformat dmy
go
That will set the date format for your database, and that should get your DATEPART function working as expected.
SET DATEFORMAT (MSDN)

Resources