perkes456
perkes456

Reputation: 1181

Javascript get month range by month name

I have a jQuery onclick function which basically receives a month name, and from that month name, I would like to get a month range. Like for instance I'm passing a month name "May" like this:

 $('#swiperNext').click(function(e)
   {
       var currentMonth = $('.swiper-slide-active h3').html();
       console.log(currentMonth);
   });

Now I'd like to pass the current month name to a JS function and get date range for that month.

So for example May would be => 01-05-2016 - 31-05-2016, so on and so on... Can someone help me out with this ?

Upvotes: 3

Views: 8916

Answers (4)

Edward Pham
Edward Pham

Reputation: 83

You can use this function:

function getMonthFromString(monthName){
    var date = new Date(Date.parse(monthName + 1, new Date().getFullYear()));
    var firstDay = new Date(date.getFullYear(), date.getMonth(), 1).getDate();
    var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
    var month = new Date(Date.parse(monthName + 1, new Date().getFullYear())).getMonth()+1;
    var year = new Date().getFullYear();

       return firstDay + "-" + month + "-" + year + " - " + lastDay + "-" + month + "-" + year;
}

Upvotes: 1

Ales Maticic
Ales Maticic

Reputation: 1965

You can do it without any third party library like this.

I presume you are calculation for current year

var date = new Date();
var y = date.getFullYear();
var month = $('.swiper-slide-active h3').text();
var m = new Date(Date.parse(month, 1, " + y)).getMonth();

var firstDay = new Date(y, m, 1);
var lastDay = new Date(y, m + 1, 0);

EDIT: I have changed the answer to use a month name as a string

Upvotes: 6

Jack jdeoel
Jack jdeoel

Reputation: 4584

First I create month name array according as getMonth() index.Then find wanted month name by indexOf ,that will return your array index when found it.

Note getMonth() is only return number between 0 and 11 assume Jan to Dec

So added 1 in getMonth

var monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];
$('#swiperNext').click(function(e)
   {
       var currentMonth = $('.swiper-slide-active h3').html();
       var month = monthNames.indexOf(currentMonth);
       var d = new Date();
       var start = new Date(d.getFullYear(),month,1);
       var end = new Date(d.getFullYear(),month+1,0);
       var startdate = start.getDate()+"/"+(start.getMonth()+1)+"/"+start.getFullYear();
       var enddate = end.getDate()+"/"+(end.getMonth()+1)+"/"+end.getFullYear();
    console.log(startdate+"-"+enddate);
   });

Upvotes: 0

Oscar Eduardo
Oscar Eduardo

Reputation: 386

I would use Moment.js. It is perfect for manipulating dates and do all sorts of things.

In your specific case:

var startOfMonth = moment().month("June").startOf("month").toDate()
var endOfMonth   = moment().month("June").endOf("month").toDate()

Upvotes: 3

Related Questions