MomentJs : How to get start of the same week last year? - javascript

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?

Related

How can I get the dates for the current week in JavaScript?

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.

How to detect first day of a month with Day.js

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

Can I get the monthly cycle using moment.js for every months?

i want to run a query from last month's 15 January and current month's 14th February. and i want to run the logic in such a way that this follows for next months also like from 15th February to 14th March. I certainly don't know how to proceed with this problem using moment.js
like I want to get the last months' 15th date and current months 14th for each month. think like a cycle where the cycle starts from 15th of the previous months and ends with this months 14th
Given a moment instance called date the following method calculates the 15th of the previous month to the 14th of the current month (where "current" date is date parameter):
function getRange(date){
var start = date.clone().startOf("month").add(-1,'months').add(14,'days');
var end = date.clone().startOf("month").add(13,"days");
return [start,end];
}
A live example with 2 test cases can be seen in the demo below.
var testArr = [
moment(),
moment({y:2019,M:11,d:31}) // 31 december 2019. (Yes months are zero based!)
];
for(var i=0;i<testArr.length;i++){
var [start,end] = getRange(testArr[i]);
console.log(start,' --> ',end);
}
function getRange(date){
var start = date.clone().startOf("month").add(-1,'months').add(14,'days');
var end = date.clone().startOf("month").add(13,"days");
return [start,end];
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
this problem will be solved in this way I am adding simplicity in #Jamiec
const date = moment();
const start = date.clone().startOf("month").add(-1,'months').add(14,'days');
const end = date.clone().startOf("month").add(13,"days");
console.log(start,end)

Moment.js get the week number based on a specific day (also past years)

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")

Last month's last date using Javascript

I have a textbox in which I show only month and year. So when I have June 2013, and when I look for First half, then I want the previous month's date to 14th of June.
So in Javascript I was simply using
var currentmonth=doucument.getElementbyId("textbox");
var currentdate="01-"+currentmonth;// as earlier I had 1st to 15 of month.
I want to get the last months' last date that too in a format, like
"31-May-2013".
I am trying to change like:
currentdate= new Date(currentdate).get...//but not working
Create date object:
var currentdate = new Date("01-"+currentmonth)
Set last day of month using setFullYear, getFullYear and getMonth methods:
currentdate.setFullYear(test.getFullYear(), test.getMonth()+1, 0)

Categories