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.
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With