How to get 1st and last date of a Month and Year where Month Is in Alpabet [closed]


How to get 1st and last date of a Month and Year where Month Is in Alpabet [closed]



i have a textbox in which date is coming as March 2016
.now i want 1st and last date of that Month


textbox



Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.





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: stackoverflow.com/questions/222309/…
– David Angulo
Jul 2 at 5:12




3 Answers
3



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);





how to seprate Month and Year from text box ?
– Shrey
Jul 2 at 5:20





check updated answer
– Nitin Sawant
Jul 2 at 5:23




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





i have the month and Year in the Textbox i want to get 1st and last date of the month by that !!
– Shrey
Jul 2 at 5:16





read the values from textbox and pass to the function
– Nitin Sawant
Jul 2 at 5:17





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 2 at 5:19




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 am getting Input as March 2016 Feb 2016
– Shrey
Jul 2 at 5:21





Add only getTheDays () to your code and it will work.
– i_th
Jul 2 at 5:42





in your textbox add onmouseout="getTheDays()"
– i_th
Jul 2 at 5:43





@Shrey Right now ,it works with only input mm yyyy
– i_th
Jul 2 at 5:52

Popular posts from this blog

How to add background colour in existing image using Swift?

Moria Casán

How to make file upload 'Required' in Contact Form 7?