javascript get month and year from date code example

Example 1: javascript get day of year

// `date` is a Date object
const dayOfYear = date => Math.floor((date - new Date(date.getFullYear(), 0, 0)) / (1000 * 60 * 60 * 24));

// Example
dayOfYear(new Date(2020, 04, 16));      // 137

Example 2: get year from date javascript

document.write(new Date().getFullYear());

Example 3: how to get only month and year in js

let dateObj = new Date();

let myDate = (dateObj.getUTCFullYear()) + "/" + (dateObj.getMonth() + 1)+ "/" + (dateObj.getUTCDate());

Example 4: get current month of year in number javascript

var today = new Date();
var month = today.getMonth(); // Returns 9
console.log(month); // Output: 9