Get First and Last day of Current Date in jQuery

Last Reply one year ago By pandeyism

Posted one year ago

hi

I want to learn how to get first and last day of current month in datepicker

so the result should be 

2019-04-01

2019-04-30

its not working. first and last day not correct calculate

var bdate = new Date();
var edate = new Date();
bdate.setMonth(bdate.getMonth(), 1);
edate.setMonth(edate.getMonth() + 1, 0);

var firstDayofyear = bdate.toISOString().slice(0, 10);
var lastDayofyear = edate.toISOString().slice(0, 10);

$('#datepickerbeg').val(firstDayofyear);
$('#datepickerend').val(lastDayofyear );

 

Posted one year ago Modified on one year ago

Hi alya14,

Refer below it's working.

HTML

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        var bdate = new Date();
        var edate = new Date();
        bdate.setMonth(bdate.getMonth(), 1);
        edate.setMonth(edate.getMonth() + 1, 0);
        var firstDayofyear = bdate.toISOString().slice(0, 10);
        var lastDayofyear = edate.toISOString().slice(0, 10);
        $('#datepickerbeg').val(firstDayofyear);
        $('#datepickerend').val(lastDayofyear);
    });
</script>
<div>
    <input type="text" id="datepickerbeg" />
    <br />
    <input type="text" id="datepickerend" />
</div>

Demo