M A K Ripon M A K Ripon - 6 months ago 10
Javascript Question

How to get a year first date and last date in javascript

Need to get first date and last date of a year from passing only year number. It's almost look like below.

var year = 2016 //Any Year
var first_date = ? //From Year
var last_date = ? //From Year


Solution: OK I got my solution. Sharing it here for others.

var year = 2016 //Any Year
var first_date = new Date(year, 0, 1, 00, 00, 00, 00);
var last_date = new Date(year, 11, 31, 23, 59, 59, 59);

Answer

Generate using Date() method, either with valid date string

var year = 2016 //Any Year
var first_date = new Date('1 January ' + 2016 + ' 00:00:00') //From Year
var last_date = new Date('31 December ' + 2016 + ' 00:00:00') //From Year

document.write(first_date + '<br>' + last_date)


Or as parameter like new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]); format

var year = 2016 //Any Year
var first_date = new Date(2016 ,0,1) //From Year
var last_date = new Date(2016,11,31) //From Year

document.write(first_date + '<br>' + last_date)

Comments