How to Get the Last Day of the Month

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

How do I get the last day of a month?

The last day of the month you get like this, which returns 31:

DateTime.DaysInMonth(1980, 08);

How to find the last day of the month from date?

t returns the number of days in the month of a given date (see the docs for date):

$a_date = "2009-11-23";
echo date("Y-m-t", strtotime($a_date));

How to get the last day of the month?

calendar.monthrange provides this information:

calendar.monthrange(year, month)

    Returns weekday of first day of the month and number of days in month, for the specified year and month.

>>> import calendar
>>> calendar.monthrange(2002, 1)
(1, 31)
>>> calendar.monthrange(2008, 2) # leap years are handled correctly
(4, 29)
>>> calendar.monthrange(2100, 2) # years divisible by 100 but not 400 aren't leap years
(0, 28)

so:

calendar.monthrange(year, month)[1]

seems like the simplest way to go.

How to get last day of the month

This will give you last day of current month.

var t= new Date();
alert(new Date(t.getFullYear(), t.getMonth() + 1, 0, 23, 59, 59));

Get the first and last day of current month in Go/Golang?

You can use now library, it really simple :

now.BeginningOfMonth()    // 2013-11-01 00:00:00 Fri
now.EndOfMonth() // 2013-11-30 23:59:59.999999999 Sat

Please take a look here for detail : https://github.com/jinzhu/now

JavaScript get last day of Month - based on Year, Month and day

Create a Date instance with the last day of the month then work backwards one day at a time until you match the day of the week you're after

const normaliseDay = day => day.trim().toLowerCase()
const dayIndex = new Map([
["sunday", 0],
["monday", 1],
["tuesday", 2],
["wednesday", 3],
["thursday", 4],
["friday", 5],
["saturday", 6],
])

const getLastDayOfMonth = (year, month, dow) => {
const day = dayIndex.get(normaliseDay(dow))

// init as last day of month
const date = new Date(Date.UTC(parseFloat(year), parseFloat(month), 0))

// work back one-day-at-a-time until we find the day of the week
while (date.getDay() != day) {
date.setDate(date.getDate() - 1)
}

return date
}

console.log(getLastDayOfMonth("2022", "02", "Wednesday"))

Getting last day of the month in a given string date

Java 8 and above.

By using convertedDate.getMonth().length(convertedDate.isLeapYear()) where convertedDate is an instance of LocalDate.

String date = "1/13/2012";
LocalDate convertedDate = LocalDate.parse(date, DateTimeFormatter.ofPattern("M/d/yyyy"));
convertedDate = convertedDate.withDayOfMonth(
convertedDate.getMonth().length(convertedDate.isLeapYear()));

Java 7 and below.

By using getActualMaximum method of java.util.Calendar:

String date = "1/13/2012";
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date convertedDate = dateFormat.parse(date);
Calendar c = Calendar.getInstance();
c.setTime(convertedDate);
c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));


Related Topics



Leave a reply



Submit