How to Find the Last Day of the Month from Date

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

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 to get the last day of the month from a given date

If you have a date d then the simplest way is to use the calendar module to find the number of days in the month:

datetime.date(d.year, d.month, calendar.monthrange(d.year, d.month)[-1])

Alternatively, using only datetime, we just find the first day of the next month and then remove a day:

datetime.date(d.year + d.month // 12, 
d.month % 12 + 1, 1) - datetime.timedelta(1)

You might find the logic clearer if expressed as:

datetime.date(d.year + (d.month == 12), 
(d.month + 1 if d.month < 12 else 1), 1) - datetime.timedelta(1)

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

How get the last day of a month of a list of dates

Where other solutions only use the .month value, you might miss the differences across different years. Grouping across .year and .month can avoid this. I'm not sure if this is what you require

df = pd.DataFrame(dict(dates=pd.date_range(start=pd.datetime(2017, 10, 1), periods=20, freq='3W')))
Out[]:
dates
0 2017-10-01
1 2017-10-22
2 2017-11-12
3 2017-12-03
4 2017-12-24
5 2018-01-14
6 2018-02-04
7 2018-02-25
8 2018-03-18
9 2018-04-08
10 2018-04-29
11 2018-05-20
12 2018-06-10
13 2018-07-01
14 2018-07-22
15 2018-08-12
16 2018-09-02
17 2018-09-23
18 2018-10-14
19 2018-11-04


df.groupby([df.dates.dt.year, df.dates.dt.month]).max()
Out[]:
dates
dates dates
2017 10 2017-10-22
11 2017-11-12
12 2017-12-24
2018 1 2018-01-14
2 2018-02-25
3 2018-03-18
4 2018-04-29
5 2018-05-20
6 2018-06-10
7 2018-07-22
8 2018-08-12
9 2018-09-23
10 2018-10-14
11 2018-11-04

df.groupby([df.dates.dt.year, df.dates.dt.month]).max().values
Out[]:
array([['2017-10-22T00:00:00.000000000'],
['2017-11-12T00:00:00.000000000'],
['2017-12-24T00:00:00.000000000'],
['2018-01-14T00:00:00.000000000'],
['2018-02-25T00:00:00.000000000'],
['2018-03-18T00:00:00.000000000'],
['2018-04-29T00:00:00.000000000'],
['2018-05-20T00:00:00.000000000'],
['2018-06-10T00:00:00.000000000'],
['2018-07-22T00:00:00.000000000'],
['2018-08-12T00:00:00.000000000'],
['2018-09-23T00:00:00.000000000'],
['2018-10-14T00:00:00.000000000'],
['2018-11-04T00:00:00.000000000']], dtype='datetime64[ns]')

Get last day of month in python

You can't make a datetime with a month of 13. So you have to find a way to fix it. A simple solution is to convert the incremented month to an extra year:

# Reduce 12 to 1, 0 and all other #s to 0, #
extrayear, month = divmod(cur_ds.month, 12)
# Add 1 or 0 to existing year, add one to month (which was reduced to 0-11)
next_month = datetime(year=cur_ds.year + extrayear, month=month + 1, day=1)

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 can I get the last day of the month in C#?

Another way of doing it:

DateTime today = DateTime.Today;
DateTime endOfMonth = new DateTime(today.Year,
today.Month,
DateTime.DaysInMonth(today.Year,
today.Month));

php get last day of given month

I have had this problem with PHP before, try the following way:

$dateToTest = "2015-02-01";
$lastday = date('t',strtotime($dateToTest));


Related Topics



Leave a reply



Submit