how to get the last day of the month from a given date

I have a column of dates in my dataframe and I'd like to get the last day of the month from the dates example, if the date is '2017-01-25' I want to get '2017-01-31' I suppose I can get the month and year number from the dates and use monthrange to figure out the last day of the month but I'm looking for a one line code

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)

Excel formula: Get last day of month, How do you figure out the last day of the month? There is another method to find the last day of the current month, that is by using the DATE function. Enter the formula in cell C2 =DATE (YEAR (A2),MONTH (A2)+1,0) Copy down the formula from cell C2 to the range C3:C10. You will get the desired result.

Finding the last date of a month with

year, month = 2017, 2
pd.date_range('{}-{}'.format(year, month), periods=1, freq='M')

Excel MONTH function, How do I get the last day of the month in Excel? When you supply zero as the day argument to DATE, the date function will "roll back" one day to the last day of the previous month. So, by adding 1 to the month, and using zero for day, DATE returns the last day of the "original" month.

I get the first of the month and minus one day to get the last day of the month. ccyymmdd = str((pd.Period(datetime.today().replace(day=1), 'D') - 1).strftime("%C%y-%m-%d"))

Finding the Last Day of a Given Month in Excel, How do I get the last month from a previous date in Excel? This is a short tutorial on how to get the last day / date of a given month using PHP. In the code snippets below, I will show you how to get the last date of this month, as well as the last day of a month from a specified date. The key to getting the last date of a month is the letter “t”.

EOMONTH function, Finding the Last Day of a Given Month in Excel. Enter the formula in cell B2 =EOMONTH(A2,0) Since we need the last day of the current month, the 2nd parameter in this function will be 0. For example, the given date is 04/04/1924 and I want to find out the last day of February of the year 1924. I came up with the add_month but it seems not flexible if I have different given month f

Excel Formula, Returns the serial number for the last day of the month that is the indicated number of months before or after This requires a start date and the use of months. 24. This should work: $week_start = strtotime('last Sunday', time()); $week_end = strtotime('next Sunday', time()); $month_start = strtotime('first day of this month', time()); $month_end = strtotime('last day of this month', time()); $year_start = strtotime('first day of January', time()); $year_end = strtotime('last day of December', time()); echo date('D, M jS Y', $week_start).'<br/>'; echo date('D, M jS Y', $week_end).'<br/>'; echo date('D, M jS Y', $month_start).'<br/>'; echo date('D, M

How to get the last day of the month?, All these cells have been formatted with the custom number format "dd DATE - The date as a date serial number given a year, month, day. 6. You can find the last date of any month by this code: var now = DateTime.Now;var startOfMonth = new DateTime(now.Year, now.Month, 1);var DaysInMonth = DateTime.DaysInMonth(now.Year, now.Month);var lastDay = new DateTime(now.Year, now.Month, DaysInMonth); share|improve this answer|follow |.

Comments
  • Since you mention dataframes, I'm assuming you're using pandas? Because if so, this question is basically a dupe of this: you can do df['column_with_dates'] + pd.offsets.MonthEnd().
  • Hi, welcome to StackOverflow. In my view this logic is already covered by one of the options in the accepted answer. If you disagree, please edit your answer to explain why.