Get First and Last Date of Current Month with JavaScript or Jquery

Get first and last date of current month with JavaScript or jQuery

Very simple, no library required:

var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);

or you might prefer:

var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDay = new Date(y, m, 1);
var lastDay = new Date(y, m + 1, 0);

EDIT

Some browsers will treat two digit years as being in the 20th century, so that:

new Date(14, 0, 1);

gives 1 January, 1914. To avoid that, create a Date then set its values using setFullYear:

var date = new Date();
date.setFullYear(14, 0, 1); // 1 January, 14

Get the first and last date of the previous month in JQuery

That's the first day of the current month new Date(now.getFullYear(), now.getMonth(), 1) to get the last day of the previous month create a date 1-day earlier: new Date(now.getFullYear(), now.getMonth(), 1 - 1).

To get the first day of the previous month we should substract 1 from the month component new Date(now.getFullYear(), now.getMonth() - 1, 1) but there is an issue if the current month is January (0) and the previous month is December (11). Hence I wrapped the month expression creating a cycle so it always returns a positiove value.

var now = new Date();var prevMonthLastDate = new Date(now.getFullYear(), now.getMonth(), 0);var prevMonthFirstDate = new Date(now.getFullYear() - (now.getMonth() > 0 ? 0 : 1), (now.getMonth() - 1 + 12) % 12, 1);
var formatDateComponent = function(dateComponent) { return (dateComponent < 10 ? '0' : '') + dateComponent;};
var formatDate = function(date) { return formatDateComponent(date.getMonth() + 1) + '/' + formatDateComponent(date.getDate()) + '/' + date.getFullYear();};
document.write(formatDate(prevMonthFirstDate) + ' - ' + formatDate(prevMonthLastDate));

Calculate last day of month

var month = 0; // January
var d = new Date(2008, month + 1, 0);
console.log(d.toString()); // last day in January

Moment js get first and last day of current month

In case anyone missed the comments on the original question, you can use built-in methods (works as of Moment 1.7):

const startOfMonth = moment().startOf('month').format('YYYY-MM-DD hh:mm');
const endOfMonth = moment().endOf('month').format('YYYY-MM-DD hh:mm');

how to get first date and last date from month and year in javascript

Try the following to get your expected output:

function GetFirstAndLastDate(year, month)
{
var firstDayOfMonth = new Date(year, month-1, 2);
var lastDayOfMonth = new Date(year, month, 1);

console.log('First Day: ' + firstDayOfMonth.toISOString().substring(0, 10));
console.log('Last Day: ' + lastDayOfMonth.toISOString().substring(0, 10));

}

GetFirstAndLastDate(2021, 10);

Trying to get first date of current month in html date picker

date input fields must be in YYYY-MM-DD format. Your code:

var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);

Will give back a string, e.g. Tue Sep 01 2020 00:00:00 GMT-0400 (Eastern Daylight Time), which is not what you want.

You can combine a couple of existing StackOverflow answers to accomplish what you want:

// https://stackoverflow.com/a/23593099/378779
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();

if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;

return [year, month, day].join('-');
}

// https://stackoverflow.com/a/13572682/378779
function getFstDayOfMonFnc() {
var date = new Date();
return new Date(date.getFullYear(), date.getMonth(), 1)
}

Assuming fdate should be the first of the month and tdate should be today's date:

document.getElementById('fdate').value = formatDate( getFstDayOfMonFnc() );
document.getElementById('tdate').value = formatDate( new Date() );

How to get 1st and last date of a Month and Year where Month Is in Alphabet

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

get the first and last day of the month

You have to consider the Timezone offset.
https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Date/getTimezoneOffset

function firstAndLast() {
const offset = new Date().getTimezoneOffset() / 60

const startDate = new Date(2020, 8, 1, 0-offset);
const endDate = new Date(2020, startDate.getMonth() + 1, 0, 0-offset)

console.log('startDate', startDate) // <-- 2020-08-31T22:00:00.000Z
console.log('endDate', endDate) // <-- 2020-09-29T22:00:00.000Z
}

firstAndLast();


Related Topics



Leave a reply



Submit