2

Financial Year

I want to get financial year by using Jquery, Check the example as i have 12 fields and every field is having a month name and in the same row it also shows the 1st and last date of month, some how i reached at the point but unable to get such results.

var theMonths = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

for (i = 0; i < 12; i++) {
  var date = new Date();
  var firstDay = new Date(date.getFullYear(), date.getMonth(theMonths[i]), 1);
  var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
  var months = '<li class="' + theMonths[i] + '">' + theMonths[i] + ' - - - - - -  ' + firstDay + '---------' + lastDay + '</li>';
  $('.calenderYear .month').append(months);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="calenderYear">
  <ul class="month"></ul>
  <span class="dates"></span>
</div>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Salman Khan Sohoo
  • 97
  • 1
  • 1
  • 13

1 Answers1

0

The issue is because you're providing a text value from your array to the month parameter of the Date object constructor. That value should be an integer.

Therefore your array is redundant, you can just pass i and i + 1 respectively.

Also note that your output starts at July with the Financial year. To do that you can add an offset to the iterating variable, and use the modulo operator to ensure it wraps around to the starting months of the following year.

Finally, The lastDay should technically be at 23:59:59, not 00:00:00, otherwise there's an almost 24 hour period which isn't covered.

Try this:

var theMonths = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var offset = 6;

for (i = 0; i < 12; i++) {
  var date = new Date();
  var monthIndex = i + offset;
  var firstDay = new Date(date.getFullYear(), monthIndex % theMonths.length, 1);
  var lastDay = new Date(date.getFullYear(), monthIndex + 1 % theMonths.length, 0, 23, 59, 59);
  var months = '<li class="' + theMonths[i] + '"><p>' + theMonths[monthIndex % theMonths.length] + '</p><p>' + firstDay + '</p><p>' + lastDay + '</p></li>';
  $('.calenderYear .month').append(months);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="calenderYear">
  <ul class="month"></ul>
  <span class="dates"></span>
</div>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • Thank you buddy you saved my life :) – Salman Khan Sohoo Apr 27 '17 at 07:12
  • One thing more, can you please let me know how to get this auromatically and how can i format date only, as it show standard time etc. and also if i have a text field where i give only month (any) it starts from that month – Salman Khan Sohoo Apr 27 '17 at 07:13
  • Date formatting is a pain in JS. I'd suggest you use a library such as Date.js or Moment.js. You can do it manually though: http://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date – Rory McCrossan Apr 27 '17 at 07:15
  • Is it posible to do it without library? and get financial year with custom given month, – Salman Khan Sohoo Apr 27 '17 at 07:29
  • Brother your code works very fine but when new year starts it shows January 2018 but it shows Undefined and adter it show the date which is really fine. please help, – Salman Khan Sohoo Apr 27 '17 at 07:35
  • Thank you very much dear, its great now please help me in generating auto month with given custom month name, suppose if i give January then year starts from January and generate remaining months, or if i give August so it starts from August and generate remaining months. also formating the date please without any library. Thanks – Salman Khan Sohoo Apr 27 '17 at 07:45