Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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')); 
like image 731
user3214545 Avatar asked Feb 17 '14 16:02

user3214545


People also ask

How do you find the current year using moments?

Calling format('YYYY') will invoke moment's string formatting functions, which will parse the format string supplied, and build a new string containing the appropriate data. Since you only are passing YYYY , then the result will be a string containing the year. If all you need is the year, then use the year() function.


1 Answers

You could do this:

moment().startOf('year'); 

format by doing something like this:

moment().startOf('year').format('MM/DD/YYYY'); 
like image 178
m007 Avatar answered Sep 23 '22 19:09

m007