I am building a calendar component in React without using any library but Day.js.
I need to detect the first weekday of the month in order to leave a gap as shown in the picture. Otherwise the first day of every month has to be Sunday. How can I detect the first weekday of the given date?
Day.js has built in function to achieve this
dayjs().startOf("month").day()
This method returns current month's first week day as number.
0 = Sunday,
6 = Saturday
Related
I'd like to be able to get the first and the last date of the current week. For example, this week would be September 4th to September 10th.
The issue I'm running into happens at the end of the month when there are dates from two months (like the last month of August). This caused a problem because the date range was displayed as August 28th to August 3rd when it should've been September 3rd.
I saw some other posts recommending Moment.js, but the Docs say that it shouldn't be used in new projects. What's a good way to do this?
Get the current date: const start = new Date();
Shift that back by the current day-of-week: start.setDate(start.getDate() - start.getDay());
Make a new date based on that: const end = new Date(start);
Shift that date forward: end.setDate(end.getDate() + 6);
That will give you a Sunday to Saturday week. You can shift the days as necessary based on what you consider a "week" to be.
The JavaScript Date API will deal with month shifts automatically. Thus if it's Thursday September 1, moving the day-of-month back to Sunday will correctly give the date in August.
I have one problem. Can you tell me how to check does it day in the current week?
I am working on some service for a weather forecast. I have a current day but must check does it in the current week. Only what I know is that I must use 'isSame' function from Moment JS.
This is my line of code.
if(this.conversation.payload.grain==="week" && moment().startOf('week').isSame(this.conversation.payload.forecastTime))
"forecastTime" is a current day. However, the condition is not good and does not enter the if loop.
Thank you!
This is assuming your forecastedDate is an actual javascript date or a moment object.
The isSame function also takes in a granularity parameter.
Just add the 'week' parameter to the isSame method like so:
if(this.conversation.payload.grain==="week" &&
moment().startOf("week").isSame(this.conversation.payload.forecastTime, "week"))
To get the day of the week is easily done with the momentjs library.
This will give you a day of the week based on the locale:
var dayOfWeek = moment(this.conversation.payload.forecastTime).weekday()
For example, if the locale uses Monday as the first day of the week then 0 = Monday and 6 = Sunday. Please keep in mind that the value you recieve will change based on the current locale.
If you don't want the value to change based on locale but always want to receive a Monday-Sunday value use isoWeekday():
var dayOfWeek = moment(this.conversation.payload.forecastTime).isoWeekday()
This will give you an integer 1-7. 1 = Monday, 7 = Sunday.
So, for example, if the above code returned 4, you would know that it was Thursday.
For more details on the weekday(), isoWeekday, and day() functions, check out the momentjs docs.
I need to make a navigable calendar and i'm using angular moment library for date calculus.
I have the calendar but I need to make the previous and next month buttons. For now, I managed to find out the previous month but when I call the previous function, it will always return August (since it's subtracting 1 month from the current one I guess). How do I make this recursively using angular moment?
This is what I have and it will always return the previous month from september in milliseconds (it has to be in milliseconds).
This is my code:
$scope.firstDayOfTheMonth = moment().subtract(1, 'months').startOf('month').valueOf();
And I need to get the first day of the previous month recursively.
I need function for getting the start of the same week last year.
For example I have date : 2016-01-10 and it's the first date of the 3d week in 2016, so I need the first day of the 3d week of 2015 and it would be 2015-01-11
(I always need the first day and let's consider that I allways get the first dat of week as param for my function).
I have created such function:
var getTheSameWeekLastYear = function (date) {
var date = moment(date).startOf("week"),
weekNo = date.week();
// move a year ago and set the same week
return date.add(-1, "y").week(weekNo).startOf("week");
}
And it works fine until I got week wich starts in last year and end this year. For example, week#1 2016 - starts 2015-12-27 and ends 2016-01-02.
I this case I am getting thee same week for a year before last year - 2013-12-29.
JSFiddle
What is the best way to solve this problem?
Probably momentjs has any built in function for my task?
How could I get from moment JS the week number from a date in the past only from a moment formatted object from a day selected?
$(document).ready(function(){
var weeknumber = moment("12-25-1995", "MM-DD-YYYY").week();
console.log(weeknumber);
});
According momentjs docs:
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.
For example, in the United States, Sunday is the first day of the
week. The week with January 1st in it is the first week of the year.
So, if you are having problems getting the right week number use .isoWeek()
$(document).ready(function(){
var weeknumber = moment("11-26-2016", "MMDDYYYY").isoWeek();
alert(weeknumber);
});
Example
You can also use format()
Examples:
moment().format('w') // as .week() like '1'
moment().format('W') // as .isoWeek() like '1'
moment().format('ww') // as .week() (2 digits) like '01'
moment().format('WW') // as .isoWeek() (2 digits) like '01'
ISO Week date: https://en.wikipedia.org/wiki/ISO_week_date
More info: https://momentjs.com/docs/#week-year-week-and-weekday-tokens
to get week number by current date
moment(moment().toDate(), "MM-DD-YYYY").isoWeek()
This is not relevant to the question but if you want to get 1 for Monday, 2 for Tuesday, etc
moment().format("d")