Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to calculate first monday of the month in js

function lastDayOfMonth(y,m,dy) {
 var  days = {sun:0,mon:1,tue:2,wed:3,thu:4,fri:5,sat:6}
     ,dat = new Date(y+'/'+m+'/1')
     ,currentmonth = m
     ,firstday = false;
  while (currentmonth === m){
    firstday = dat.getDay() === days[dy] || firstday;
    dat.setDate(dat.getDate()+(firstday ? 7 : 1));
    currentmonth = dat.getMonth()+1 ;
  }
  dat.setDate(dat.getDate()-7);
  return dat;
 }
// usage 
lastDayOfMonth(2012,2,'tue'); //=>Tue Feb 28 2012 00:00:00 GMT+0100
lastDayOfMonth(1943,5,'fri'); //=>Fri May 28 1943 00:00:00 GMT+0200
Comment

PREVIOUS NEXT
Code Example
Javascript :: shorthand arrow function 
Javascript :: match if 
Javascript :: Group item by date 
Javascript :: set get variable in url 
Javascript :: Recursion In DOM 
Javascript :: javascript Using Math.max() on an Array 
Javascript :: web storage api 
Javascript :: Format javascript date with date.js library 
Javascript :: string to array in js 
Javascript :: javascript object/function which you want to proxy 
Javascript :: prototype, __proto__ 
Javascript :: javascript advanced interview questions 
Javascript :: sequelize queryinterface select 
Javascript :: react native image swiper 
Javascript :: foreach await js 
Javascript :: how to remove an object from javascript array 
Javascript :: how to make popup modal in jquery with example 
Javascript :: Creating New Block for blockchain 
Javascript :: simple chat app 
Javascript :: notify js 
Javascript :: some in js 
Javascript :: what is a heap in javascript 
Javascript :: react native charts 
Javascript :: javascript filter method 
Javascript :: javascript dict 
Javascript :: http error 406 
Javascript :: javascript static class variable 
Javascript :: vue on page link or anchor 
Javascript :: angularjs popup 
Javascript :: Document object not defined Next js 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =