Q:

how to get the year in javascript

var currentYear= new Date().getFullYear(); 
12
new Date().getFullYear(); // This will get you the current year
4

var d = new Date();

var n = d.getFullYear();
 
3
// Return today's date and time
var currentTime = new Date()

// returns the month (from 0 to 11)
var month = currentTime.getMonth() + 1

// returns the day of the month (from 1 to 31)
var day = currentTime.getDate()

// returns the year (four digits)
var year = currentTime.getFullYear()

// write output MM/dd/yyyy
document.write(month + "/" + day + "/" + year)
1
// `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
2
document.write(new Date().getFullYear());
0

New to Communities?

Join the community