Ahsan Mani
Ahsan Mani

Reputation: 31

How to create array objects of fullyear months with date and day?

I want to get an array of object which include months as a key date and day as a object and child of array and this will be value of month

i want some thing like this,

 var fullYearCalendar = [
                          January:[{date:'1',day:'tue'},
                                   {date:'2',day:'wed'},
                                   {date:'3',day:'thr'},
                                   and so on...
                                  ],
                          February:[{date:'8',day:'sun'},
                                   {date:'9',day:'mon'},
                                   {date:'10',day:'tue'},
                                   and so on...
                                  ],
                                  and so on...
                        ]

Upvotes: 0

Views: 987

Answers (2)

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48610

You can instantiate a Date to start at a specific year and then you can just increment day-by-day, until you reach the following year.

Edit: Added custom localization options.

const DAY_IN_MILLIS = 24 * 60 * 60 * 1000;
const defaultLocalizationOptions = {
  monthNames : [
    "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"
  ],
  dayNames : [
    "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
  ]
};
let myLocOpts = { dayNames : [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ] };

console.log(getCalendar(2020, myLocOpts)); // Get a calendar for the year 2020

function getCalendar(year, localizationOptions) {
  // Merge default and custom localization options.
  let locOpts = Object.assign({}, defaultLocalizationOptions, localizationOptions);
  let currDate = new Date(Date.UTC(year, 0, 0, 0, 0, 0));
  addDay(currDate); // Add a day
  let calendar = {};
  while (currDate.getUTCFullYear() < year + 1) {
    let month = locOpts['monthNames'][currDate.getUTCMonth()];
    let daysOfMonth = calendar[month] || [];
    daysOfMonth.push({
      date : currDate.getUTCDate(),
      day : locOpts['dayNames'][currDate.getUTCDay()]
    });
    calendar[month] = daysOfMonth;
    addDay(currDate);
  }
  return calendar;
}

function addDay(date) {
  date.setTime(date.getTime() + DAY_IN_MILLIS); // Add a day
  return date;
}
.as-console-wrapper { top: 0; max-height: 100% !important; }

Upvotes: 1

Brian
Brian

Reputation: 1898

It's fairly easy to do so with the Date object. One thing you will need to do keep in mind are leap years (which contain 366 days):

let fullYearCalendar = {};

// initialize date to Jan 1, 1900
let date = new Date(0, 0, 1);

// day length in milliseconds
let dayLength = 1000 * 60 * 60 * 24;

// year length in days (account for leap years)
let year = date.getFullYear();
let yearLength = ((year % 4) || (!(year % 100) && (year % 400))) ? 365 : 366;

for (let i = 0; i < yearLength; i ++) {
  // determine month
  let month = date.toLocaleDateString('en-US', {month: 'long'});

  // determine weekday
  let weekday = date.toLocaleDateString('en-US', {weekday: 'short'});
  
  // initialize month if it does not exist
  if (!fullYearCalendar[month])
    fullYearCalendar[month] = [];

  // add current day to month
  fullYearCalendar[month].push({
    date: date.getDate(),
    day: weekday.toLowerCase()
  })
    
  // increment date by one day
  date = new Date(date.getTime() + dayLength);
}

console.log(fullYearCalendar);

Upvotes: 1

Related Questions