Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get first day of current year in javascript? [closed]

Tags:

javascript

I need javascript code to get first day of year. e.g. It will be 1st Jan 2013 for this year.

For next year it should be 1st Jan 2014. So basically 1st day of whatever is current year.

like image 758
user1990525 Avatar asked Jan 21 '13 08:01

user1990525


2 Answers

Create a new Date instance to get the current date, use getFullYear to get the year from that, and create a Date intance with the year, month 0 (January), and date 1:

var d = new Date(new Date().getFullYear(), 0, 1); 
like image 109
Guffa Avatar answered Oct 02 '22 19:10

Guffa


var year = 2013;    var date = new Date(year, 0, 1);    console.log(date); // Tue Jan 01 2013 00:00:00 GMT+0100 (Mitteleuropäische Zeit)

You can construct Dates using new Date(YEAR,MONTH,DAY)

So giving the constructor the year you want and the first Day of the First Month, you get your Date Object

Note that the Date Object starts counting with 0 for the Month, so January == 0

like image 23
Moritz Roessler Avatar answered Oct 02 '22 19:10

Moritz Roessler