I have a textbox
in which date is coming as March 2016
.now I want 1st and last date of that Month.
Asked
Active
Viewed 223 times
-4

Cœur
- 37,241
- 25
- 195
- 267

Deepak Jain
- 137
- 1
- 3
- 27
-
I guess the first date of the month is always starts with 1? So in your case, it would be March 1, 2016. When getting the last date of the month I guess you can refer here: https://stackoverflow.com/questions/222309/calculate-last-day-of-month-in-javascript – dcangulo Jul 02 '18 at 05:12
3 Answers
1
here you can pass month name as string
var year = 2018;
var sMonthName = "March";
var iMonthNo = new Date(sMonthName + "01, "+year).getMonth();
var FirstDay = new Date(year, iMonthNo, 1);
var LastDay = new Date(year, iMonthNo + 1, 0);
console.log(FirstDay);
console.log(LastDay);
UPDATE:
here's the fiddle: http://jsfiddle.net/gtKeL/82/
var date = document.getElementById("txtDate").value.split(" ");
var year = date[1];
var sMonthName = date[0];
var iMonthNo = new Date(sMonthName + "01, "+year).getMonth();
var FirstDay = new Date(year, iMonthNo, 1);
var LastDay = new Date(year, iMonthNo + 1, 0);
console.log(FirstDay);
console.log(LastDay);

Nitin Sawant
- 7,278
- 9
- 52
- 98
0
maybe this code can help you , you dont need any library,try it
var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
by the way,some browsers only treat two digits for years

Mona
- 47
- 11
-
i have the month and Year in the Textbox i want to get 1st and last date of the month by that !! – Deepak Jain Jul 02 '18 at 05:16
-
-
well,after you set to 2016,you can show firstDay and lastDay variables in your textbox like this : var textbox = document.getElementById('textbox'); textbox3.value=answer; – Mona Jul 02 '18 at 05:19
0
http://www.encodedna.com/javascript/first-and-last-day-of-a-given-month-in-javascript.htm
<!DOCTYPE html>
<html>
<head>
<title>Get The First and Last Day of a Given Month</title>
</head>
<body>
<body>
<input type="date" id="dt" onchange="mydate1();" onmouseout="getTheDays()" hidden/>
<input type="text" id="ndt" onclick="mydate();" onmouseout="getTheDays()" hidden />
<input type="button" Value="Date" onclick="mydate();" onmouseout="getTheDays()" />
<p>First Day of the Month: <label id="fday"></label></p>
<p>Last Day of the Month: <label id="lday"></label></p>
</body>
<script>
function mydate()
{
//alert("");
document.getElementById("dt").hidden=false;
document.getElementById("ndt").hidden=true;
}
function mydate1()
{
d=new Date(document.getElementById("dt").value);
dt=d.getDate();
mn=d.getMonth();
mn++;
yy=d.getFullYear();
document.getElementById("ndt").value=mn+"/"+yy
document.getElementById("ndt").hidden=false;
document.getElementById("dt").hidden=true;
}
function getTheDays() {
// THE DATE OBJECT.
var dt = new Date(document.getElementById('dt').value );
// GET THE MONTH AND YEAR OF THE SELECTED DATE.
var month = dt.getMonth(),
year = dt.getFullYear();
// GET THE FIRST AND LAST DATE OF THE MONTH.
var FirstDay = new Date(year, month, 1);
var LastDay = new Date(year, month + 1, 0);
// FINALLY, GET THE DAY.
var weekday = new Array();
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
if (typeof weekday[FirstDay.getDay()] != 'undefined') { // CHECK FOR 'undefined'.
document.getElementById('fday').innerHTML = weekday[FirstDay.getDay()] +
' (' + FirstDay.toDateString('dd/mon/yyyy') + ')';
document.getElementById('lday').innerHTML = weekday[LastDay.getDay()] +
' (' + LastDay.toDateString('dd/mon/yyyy') + ')'; ;
}
else {
document.getElementById('fday').innerHTML = '';
document.getElementById('lday').innerHTML = '';
}
}
</script>
</html>

I_Al-thamary
- 3,385
- 2
- 24
- 37