Calculate Last Day of 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));

last day of month calculation

Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH);

This returns actual maximum for current month. For example it is February of leap year now, so it returns 29 as int.

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.

Quicksight: Aggregation Over Last Day of Month

i finally solve it like this using the posted formulas:

parseDate
(
concat
(
toString(extract("YYYY",addDateTime(1,"DD",addDateTime(1,"MM",truncDate("MM",date)))))
,"-"
,toString(extract("MM",addDateTime(1,"DD",addDateTime(1,"MM",truncDate("MM",date)))))
,"-"
,toString(extract("DD",addDateTime(-1,"DD",addDateTime(1,"MM",truncDate("MM",date)))))
)
,'yyyy-MM-dd')

And because current month have not ended so i don't have data for the the last day i use an ifelse like this:

ifelse(
parseDate(
concat(
toString(extract("YYYY",addDateTime(-1,"DD",addDateTime(1,"MM",truncDate("MM",date)))))
,"-"
,toString(extract("MM",addDateTime(-1,"DD",addDateTime(1,"MM",truncDate("MM",date)))))
,"-"
,toString(extract("DD",addDateTime(-1,"DD",addDateTime(1,"MM",truncDate("MM",date))))))
,'yyyy-MM-dd')
>= now()
,addDateTime(-1,"DD",now())
,parseDate(concat(toString(extract("YYYY",addDateTime(-1,"DD",addDateTime(1,"MM",truncDate("MM",date)))))
,"-"
,toString(extract("MM",addDateTime(-1,"DD",addDateTime(1,"MM",truncDate("MM",date)))))
,"-"
,toString(extract("DD",addDateTime(-1,"DD",addDateTime(1,"MM",truncDate("MM",date)))))
)
,'yyyy-MM-dd')
)

And finally i created de following calculated field:

sumIf(base,formatDate(date,'yyyy-MM-dd') = formatDate({fecha_last_day},'yyyy-MM-dd'))

The result:

The value related to the last day for each month, and for current month the last day with able data

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