Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get list of days in a month with Moment.js

Using Moment.js I would like to get all days in a month of specific year in an array. For example:

January-2014: [ "01-wed", "02-thr", "03-fri", "04-sat" ] 

any suggestions? I looked through Moment.js docs but couldn't find anything. The closet I got was this:

moment("2012-02", "YYYY-MM").daysInMonth()  

But this only return an int with total days for specific month not an array with each day.

like image 916
ecorvo Avatar asked Aug 31 '14 02:08

ecorvo


People also ask

How do you find day from moment date?

To get day name from date with Moment. js and JavaScript, we can use the format method. const myDate = "2022-06-28T00:00:00"; const weekDayName = moment(myDate).

How do you find the month in string in a moment?

You can simply use format('MMMM') .

What is Moment () hour ()?

The moment(). hour() Method is used to get the hours from the current time or to set the hours. Syntax: moment().hour(); or. moment().

Is moment JS being deprecated?

MomentJs recently announced that the library is now deprecated. This is a big deal for the javascript community who actively downloads moment almost 15 million times a week. With that I began a journey during a Hackathon to replace moment in a core library at my company.


2 Answers

Here's a function that will do the trick (not using Moment, but just vanilla JavaScript):

var getDaysArray = function(year, month) {   var monthIndex = month - 1; // 0..11 instead of 1..12   var names = [ 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat' ];   var date = new Date(year, monthIndex, 1);   var result = [];   while (date.getMonth() == monthIndex) {     result.push(date.getDate() + '-' + names[date.getDay()]);     date.setDate(date.getDate() + 1);   }   return result; } 

For example:

js> getDaysArray(2012,2) ["1-wed", "2-thu", "3-fri", "4-sat", "5-sun", "6-mon", "7-tue",  "8-wed", "9-thu", "10-fri", "11-sat", "12-sun", "13-mon", "14-tue", "15-wed", "16-thu", "17-fri", "18-sat", "19-sun", "20-mon", "21-tue",  "22-wed", "23-thu", "24-fri", "25-sat", "26-sun", "27-mon", "28-tue", "29-wed"] 

ES2015+ version - also hid the array of names behind a closure so it's only initialized once:

const getDaysArray = (function() {   const names = Object.freeze([ 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat' ]);   return (year, month) => {     const monthIndex = month - 1     const date = new Date(year, monthIndex, 1);     const result = [];     while (date.getMonth() == monthIndex) {       result.push(`${date.getDate()}-${names[date.getDay()]}`);     date.setDate(date.getDate() + 1);     }     return result;   } })(); 

As a side note, you can see that declaring the date const doesn't keep us from mutating it (nor would Object.freeze, used to make the weekday names array immutable, do anything to a Date). We're taking advantage of the mutability here, but if we actually wanted an immutable Date with the language enforcing that immutability in current Javascript, we'd have to go to some length.

Also note that the solutions above don't zero-pad dates before the 10th, unlike the sample output included in the question. With ES2017+ that's pretty easy to fix:

    result.push(`${date.getDate()}`.padStart(2,'0') + `-${names[date.getDay()]}`); 

Doing it in older versions of JS requires rolling your own zero-padding logic, which isn't hard but is also not really the focus of the question.

like image 179
Mark Reed Avatar answered Sep 20 '22 20:09

Mark Reed


Alternative with momentjs, working for me

function getDaysArrayByMonth() {   var daysInMonth = moment().daysInMonth();   var arrDays = [];    while(daysInMonth) {     var current = moment().date(daysInMonth);     arrDays.push(current);     daysInMonth--;   }    return arrDays; } 

And you can check

var schedule = getDaysArrayByMonth(); schedule.forEach(function(item) {   console.log(item.format("DD/MM")); }); 
like image 40
victorkurauchi Avatar answered Sep 18 '22 20:09

victorkurauchi