How to Get the Last Day of a Month

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

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

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

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

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

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)

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]')


Related Topics



Leave a reply



Submit