How to get last day of a given year with moment?

Related searches

I can hard-code the last day of a year because anyway it will always be 31st December. There are many solutions using date, js, jquery. But I'm working on an Angular project and hence my code is in typescript. My tech lead wants me to do this using moment. I'm not supposed to hard-code any date. Is there any built-in method provided by moment to fetch the last day of a given year. And I'm saying given year because in my case given year is dynamic. It can change anytime. I'm using endOf() method. I'm getting error with custom year. I mean:

const lastDayOfYear = moment().endOf('year') is working fine, but:

const lastDayOfYear = moment().endOf(2025) is giving me this error:

Argument of type '2025' is not assignable to parameter of type 'StartOf'

I also tried:

  1. const lastDayOfYear = moment().endOf("2025");
  2. year=2025; const lastDayOfYear = moment().endOf(year);

How will this method work? I've gone through the entire Moment docs. Or should I stick to Date of javascript. Something like this:

new Date(new Date().getFullYear(), 11, 31);

Please provide a solution.

PS: I want last date of a given year in MM-DD-YYYY format only.

Well if you really wanted to work that out with moment instead of using `12-31-${year}` you could instead use:

moment([year]).endOf('year').format('MM-DD-YYYY')

Moment js get first and last day of current month, Will be great to be able to get/set moment instance to last day of the moment( year-month-day). You may get last day of month. like this. There is another method to find the last day of the current month, that is by using the DATE function. Enter the formula in cell C2 =DATE (YEAR (A2),MONTH (A2)+1,0) Copy down the formula from cell C2 to the range C3:C10. You will get the desired result

moment(date).endOf('year');

Where date is a Date somewhere in that year.

Get last day of the month? � Issue #242 � moment/moment � GitHub, Moment JS start and end of given month, Don't really think there is some first and last day of that month in the var firstDay and lastDay by getting the year and� On the same way, if you want to return the last day of the current month, we have to build a formula to display the first month of the next month =DATE (YEAR (TODAY ()),MONTH (TODAY ())+1,1) And subtract 1 one more time. =DATE (YEAR (TODAY ()),MONTH (TODAY ())+1,1)-1

set current date in moment new Date()

const now = moment(new Date()).endOf('year');
alert(now);

Docs, iso week year � 17 weeks in year � 18 iso weeks in year � 19 get � 20 set � 21 max � 22 min. 03 manipulating. 00 intro � 01 add � 02 subtract � 03 start of � 04 end of� How do I get the first and last day and time of the current month in the following format in moment.js: 2016-09-01 00:00 I can get the current date and time like this: moment().format('YYYY-MM-DD h:m') which will output in the above format.

Moment js get first and last day of current month, Given a month and the task is to determine the first and last day of that month in proper format using JavaScript. JavaScript getDate() Method: This method returns the day of the month (from 1 to 31) for the defined date. Syntax: Date.getDate() Return value: It returns a number from 1 to 31, representing the day of the month.

Day of Year, The figure shows how to get the last date in February for each year given in order to see which years are leap years. As you look at the figure, keep in mind that you can use the formula to get the last day of any month, not just February. The DATE function allows you to build a date on the fly using three arguments: the year, the month, and

The program shows that you can find the first day of the previous month, the last day of the previous month, the middle of the previous month, or an anniversary of the specified date. In particular, the program answers the programmer's question by showing a concise "one-liner" that you can use to get the first and last days of the previous month.

Comments
  • const lastDayOfYear = moment().endOf('year') is working fine Can you tell us what exactly 'year' is in this context
  • @bhaskar, year is predefined constant by moment itself, which will always refer to the current year i.e. 2020. there are other parameters also you can pass. For eg. 'week', 'day', etc.
  • Don't think that is possible.. Because a constant in JS will not be passes as a string. Given the answer below, I think the endOf() function takes a string argument which defines what part of the date you need the end of..
  • If year=2025, output is: > Date is: 12-31-4045
  • Edited @Tanzeel
  • Sir, this solution worked. I'm going to use this only. Can you please explain me a little because I'll have to justify and my tech lead may do cross questions. Hope you understand what i mean. :-)
  • No Sir, I don't need +1. So i removed it from there. And now I'm ready to push the code. Thanks a lot. :-)
  • No worries @Tanzeel! In case you need to justify, moment([year]) sets the timestamp to the given year before .endOf('year') moves it to the end of the year and is then formatted by .format('MM-DD-YYYY')
  • Give me 1 min. I'll try this and come back to you. :-)
  • One small request. I tried this solution and i'm getting this output: Date is: 1609439399999. Now how do I confirm its 31st dec 2020?
  • do a new Date(1609439399999), which will return a formatted date object with that date. I checked it, that timestamp gives Dec 31 of 2020.
  • Your answer is also correct but the output is 1609439399999 while the format should be MM-DD-YYYY. Thanks anyway. I appreciate your time and efforts. :-)
  • so you can use this moment(new Date()).endOf('year').format("MM-DD-YYYY")