user3214545
user3214545

Reputation: 2213

Get the first day of the current year using Moment.js

I have two input fields to search between dates. I want the start date to default to the first day of the current year and the end date to default to today. I have the end date but I am unsure of how to get it to display the start.

var defaultStart =
var defaultEnd = moment((new Date()).valueOf());
$('#searchStart').val(defaultStart.format('DD/MM/YYYY'));
$('#searchEnd').val(defaultEnd.format('DD/MM/YYYY'));

Upvotes: 47

Views: 71159

Answers (4)

Thilina Koggalage
Thilina Koggalage

Reputation: 1084

To get the start date and end date of any given year. You can pass a year as a parameter to moment.

startDateOfTheYear = moment([year]);
endDateOfTheYear = moment([year]).endOf('year');

Upvotes: 15

m007
m007

Reputation: 1875

You could do this:

moment().startOf('year');

format by doing something like this:

moment().startOf('year').format('MM/DD/YYYY');

Upvotes: 173

E.J. Brennan
E.J. Brennan

Reputation: 46849

var defaultStart = '01/01/'+new Date().getFullYear()

Upvotes: 8

Timothy S. Van Haren
Timothy S. Van Haren

Reputation: 8966

var thisYear = (new Date()).getFullYear();    
var start = new Date("1/1/" + thisYear);
var defaultStart = moment(start.valueOf());

Upvotes: -7

Related Questions