Oracle Live SQL - Script: Generating days, months or years between dates (2023)

View All Scripts Login to Run Script

  • Script Name Generating days, months or years between dates
  • Description Examples of how to generate dates in a time period with different increment units
  • Area SQL General
  • Contributor Chris Saxon (Oracle)
  • Created Tuesday March 16, 2021
  • Statement 1

    The connect by level trick enables you to create N rows on-the-fly

    Generate rows

    select level as N from dual connect by level <= 10

    N
    12345678910

    10 rows selected.

  • Statement 2

    Using this method, you can create days by adding the level (row number) to a starting date

    Generate days

    select date'2021-01-01' + level - 1 as dt from dual connect by level <= 31

    DT
    01-JAN-2102-JAN-2103-JAN-2104-JAN-2105-JAN-2106-JAN-2107-JAN-2108-JAN-2109-JAN-2110-JAN-2111-JAN-2112-JAN-2113-JAN-2114-JAN-2115-JAN-2116-JAN-2117-JAN-2118-JAN-2119-JAN-2120-JAN-2121-JAN-2122-JAN-2123-JAN-2124-JAN-2125-JAN-2126-JAN-2127-JAN-2128-JAN-2129-JAN-2130-JAN-2131-JAN-21

    31 rows selected.

  • Statement 3

    You can use the generation trick to report number of orders per day - including days with no orders.This finds the dates of the first and last orders, then uses these to generate the days. Note the dates subquery uses RWS as its source table.

    Show orders per day

    with rws as ( select min ( order_datetime ) mn_dt, max ( order_datetime ) mx_dt from co.orders o ), dates as ( select trunc ( mn_dt ) + level - 1 dt from rws connect by level <= ( extract ( day from ( mx_dt - mn_dt ) ) + 1 ) ) select dt, count(order_datetime) from dates left join co.orders on dt <= order_datetime and dt + 1 > order_datetime group by dt order by dt

    DTCOUNT(ORDER_DATETIME)
    04-FEB-18105-FEB-18006-FEB-18007-FEB-18008-FEB-18109-FEB-18110-FEB-18111-FEB-18112-FEB-18013-FEB-18114-FEB-18015-FEB-18016-FEB-18017-FEB-18018-FEB-18019-FEB-18020-FEB-18021-FEB-18022-FEB-18223-FEB-18224-FEB-18425-FEB-18026-FEB-18227-FEB-18128-FEB-18101-MAR-18402-MAR-18203-MAR-18304-MAR-18005-MAR-18106-MAR-18207-MAR-18208-MAR-18109-MAR-18210-MAR-18211-MAR-18112-MAR-18513-MAR-18214-MAR-18415-MAR-18316-MAR-18217-MAR-18218-MAR-18219-MAR-18120-MAR-18121-MAR-18122-MAR-18523-MAR-18224-MAR-18225-MAR-181

    Rows 1 - 50. More rows exist.

  • Statement 4

    You can use the method above to find orders per day for a given customer, store, or product. The initial subquery gets the rows and columns needed to do this

    Show orders per day for a store

    with stor as ( select store_id, order_datetime from co.orders o where store_id = 23 ), rws as ( select min ( trunc ( order_datetime ) ) mn_dt, max ( trunc ( order_datetime ) ) mx_dt from stor ) select dt, count ( order_datetime ) from ( select mn_dt + level - 1 as dt from rws connect by level <= mx_dt - mn_dt + 1 ) left join stor on dt <= order_datetime and dt + 1 > order_datetime group by dt order by dt

    DTCOUNT(ORDER_DATETIME)
    22-MAR-19123-MAR-19024-MAR-19225-MAR-19226-MAR-19127-MAR-19228-MAR-19129-MAR-19030-MAR-19031-MAR-19201-APR-19302-APR-19103-APR-19204-APR-19005-APR-19106-APR-19007-APR-19008-APR-19309-APR-19010-APR-19311-APR-19312-APR-191

    22 rows selected.

  • Statement 5

    This generates all the days in the year, then filters these down to the Fridays. This means the query processes ~7x more rows than needed.

    Find the Fridays in 2021

    with rws as ( select date'2021-01-01' + level - 1 as dt from dual connect by level <= ( date'2022-01-01' - date'2021-01-01' ) ) select * from rws where to_char ( dt, 'FMDay' ) = 'Friday'

    DT
    01-JAN-2108-JAN-2115-JAN-2122-JAN-2129-JAN-2105-FEB-2112-FEB-2119-FEB-2126-FEB-2105-MAR-2112-MAR-2119-MAR-2126-MAR-2102-APR-2109-APR-2116-APR-2123-APR-2130-APR-2107-MAY-2114-MAY-2121-MAY-2128-MAY-2104-JUN-2111-JUN-2118-JUN-2125-JUN-2102-JUL-2109-JUL-2116-JUL-2123-JUL-2130-JUL-2106-AUG-2113-AUG-2120-AUG-2127-AUG-2103-SEP-2110-SEP-2117-SEP-2124-SEP-2101-OCT-2108-OCT-2115-OCT-2122-OCT-2129-OCT-2105-NOV-2112-NOV-2119-NOV-2126-NOV-2103-DEC-2110-DEC-21

    Rows 1 - 50. More rows exist.

  • Statement 6

    This calculates how many Fridays there are in the year, then only generates that many rows.This uses next_day to find the first Friday of the year. This returns the Friday after the input date, so you need to start from 31st Dec the previous year in case 1st Jan is a Friday.

    Find all the Fridays - improved

    with rws as ( select next_day ( date'2021-01-01' - 1, 'Friday' ) + ( level - 1 ) * 7 as dt from dual connect by level <= ( ( date'2021-12-31' - next_day ( date'2021-01-01' - 1, 'Friday' ) + 7 ) / 7 ) ) select dt from rws

    DT
    01-JAN-2108-JAN-2115-JAN-2122-JAN-2129-JAN-2105-FEB-2112-FEB-2119-FEB-2126-FEB-2105-MAR-2112-MAR-2119-MAR-2126-MAR-2102-APR-2109-APR-2116-APR-2123-APR-2130-APR-2107-MAY-2114-MAY-2121-MAY-2128-MAY-2104-JUN-2111-JUN-2118-JUN-2125-JUN-2102-JUL-2109-JUL-2116-JUL-2123-JUL-2130-JUL-2106-AUG-2113-AUG-2120-AUG-2127-AUG-2103-SEP-2110-SEP-2117-SEP-2124-SEP-2101-OCT-2108-OCT-2115-OCT-2122-OCT-2129-OCT-2105-NOV-2112-NOV-2119-NOV-2126-NOV-2103-DEC-2110-DEC-21

    Rows 1 - 50. More rows exist.

  • Statement 7

    Set session language to Spanish

    alter session set nls_language = Spanish

    Statement processed.

  • Statement 8

    Next_day is sensitive to NLS_LANGUAGE settings. This fails because the session now uses Spanish, so expects Viernes.The function has no parameter to override this

    Language settings for next_day

    select next_day ( date'2021-01-01', 'Friday' ) from dual

    ORA-01846: día de la semana no válido

  • Statement 9

    Set session language back to English

    alter session set nls_language = English

    Statement processed.

  • Statement 10

    Now using Spanish days-of-the-week fails.

    Language settings for next_day

    select next_day ( date'2021-01-01', 'Viernes' ) from dual

    ORA-01846: not a valid day of the week

  • Statement 11

    The number of days in a month changes, so to get a row per month you need a different method.Months_between returns the number of these from the first date to the second. This returns part-months as a decimal, calculated using a 31-day month. Add_months increments the date by N months, returning the same day-of-month as the initial date, unless the date is the last day of the month. In which case the calculated date is also the last day of the month.

    Generate months

    select add_months ( date'2021-01-01', level - 1 ) as dt from dual connect by level <= months_between ( date'2021-12-31', date'2021-01-01' ) + 1

    DT
    01-JAN-2101-FEB-2101-MAR-2101-APR-2101-MAY-2101-JUN-2101-JUL-2101-AUG-2101-SEP-2101-OCT-2101-NOV-2101-DEC-21

    12 rows selected.

  • Statement 12

    select add_months ( date'2021-01-15', level - 1 ) as dt from dual connect by level <= months_between ( date'2022-01-15', date'2021-01-15' ) + 1

    DT
    15-JAN-2115-FEB-2115-MAR-2115-APR-2115-MAY-2115-JUN-2115-JUL-2115-AUG-2115-SEP-2115-OCT-2115-NOV-2115-DEC-2115-JAN-22

    13 rows selected.

  • Statement 13

    This shows the first and last date per month in the time period, beginning on the start date. The final date is the same day of the month as the input (or last day of the month if the input is the last).

    Generate month start and end dates

    with mths as ( select add_months ( date'2021-01-15', level - 1 ) as dt from dual connect by level <= months_between ( date'2022-01-15', date'2021-01-15' ) + 1 ) select case rownum when 1 then dt else trunc ( dt, 'mm' ) end start_date, lead ( trunc ( dt, 'mm' ) - 1, 1, dt ) over ( order by dt ) end_date from mths

    START_DATEEND_DATE
    15-JAN-2131-JAN-2101-FEB-2128-FEB-2101-MAR-2131-MAR-2101-APR-2130-APR-2101-MAY-2131-MAY-2101-JUN-2130-JUN-2101-JUL-2131-JUL-2101-AUG-2131-AUG-2101-SEP-2130-SEP-2101-OCT-2131-OCT-2101-NOV-2130-NOV-2101-DEC-2131-DEC-2101-JAN-2215-JAN-22

    13 rows selected.

  • Statement 14

    This uses intervals to add the number of years to the input date.

    Generate years

    select date'2021-01-01' + numtoyminterval ( level - 1, 'year' ) as dt from dual connect by level <= ( months_between ( date'2022-12-31', date'2020-01-01' ) / 12 ) + 1

    DT
    01-JAN-2101-JAN-2201-JAN-23

    3 rows selected.

  • Statement 15

    Make these reusable with SQL macros

    create or replace package date_mgr as function generate_days ( start_date date, end_date date, day_increment integer default 1 ) return varchar2 sql_macro; function generate_months ( start_date date, end_date date, month_increment integer default 1 ) return varchar2 sql_macro; function generate_years ( start_date date, end_date date, year_increment integer default 1 ) return varchar2 sql_macro; end date_mgr; 

    Package created.

  • Statement 16

    Make these reusable with SQL macros

    create or replace package body date_mgr as function generate_days ( start_date date, end_date date, day_increment integer default 1 ) return varchar2 sql_macro as stmt varchar2(4000); begin stmt := 'select start_date + ( level - 1 ) * day_increment as dt from dual connect by level <= ( ( ( end_date - start_date ) + day_increment ) / day_increment )'; dbms_output.put_line ( stmt ); return stmt; end generate_days; function generate_months ( start_date date, end_date date, month_increment integer default 1 ) return varchar2 sql_macro as stmt varchar2(4000); begin stmt := ' select add_months ( start_date, ( level - 1 ) * month_increment ) as dt from dual connect by level <= ( months_between ( end_date, start_date ) + month_increment ) / month_increment'; dbms_output.put_line ( stmt ); return stmt; end generate_months; function generate_years ( start_date date, end_date date, year_increment integer default 1 ) return varchar2 sql_macro as stmt varchar2(4000); begin stmt := q'! select start_date + numtoyminterval ( ( level - 1 ) * year_increment, 'year' ) as dt from dual connect by level <= ( ( months_between ( end_date, start_date ) / 12 ) + year_increment ) / year_increment!'; dbms_output.put_line ( stmt ); return stmt; end generate_years; end date_mgr; 

    Package Body created.

  • Statement 17

    Generate days using SQL macro

    select * from date_mgr.generate_days ( date'2021-01-01', date'2021-01-31' )

    DT
    01-JAN-2102-JAN-2103-JAN-2104-JAN-2105-JAN-2106-JAN-2107-JAN-2108-JAN-2109-JAN-2110-JAN-2111-JAN-2112-JAN-2113-JAN-2114-JAN-2115-JAN-2116-JAN-2117-JAN-2118-JAN-2119-JAN-2120-JAN-2121-JAN-2122-JAN-2123-JAN-2124-JAN-2125-JAN-2126-JAN-2127-JAN-2128-JAN-2129-JAN-2130-JAN-2131-JAN-21

    31 rows selected.
    select start_date + ( level - 1 ) * day_increment as dt from dual connect by level <= ( ( ( end_date - start_date ) + day_increment ) / day_increment )

  • Statement 18

    Generate every Friday using SQL macro

    select * from date_mgr.generate_days ( next_day ( date'2021-01-01' - 1, 'Friday' ), date'2021-12-31', 7 )

    DT
    01-JAN-2108-JAN-2115-JAN-2122-JAN-2129-JAN-2105-FEB-2112-FEB-2119-FEB-2126-FEB-2105-MAR-2112-MAR-2119-MAR-2126-MAR-2102-APR-2109-APR-2116-APR-2123-APR-2130-APR-2107-MAY-2114-MAY-2121-MAY-2128-MAY-2104-JUN-2111-JUN-2118-JUN-2125-JUN-2102-JUL-2109-JUL-2116-JUL-2123-JUL-2130-JUL-2106-AUG-2113-AUG-2120-AUG-2127-AUG-2103-SEP-2110-SEP-2117-SEP-2124-SEP-2101-OCT-2108-OCT-2115-OCT-2122-OCT-2129-OCT-2105-NOV-2112-NOV-2119-NOV-2126-NOV-2103-DEC-2110-DEC-21

    Rows 1 - 50. More rows exist.
    select start_date + ( level - 1 ) * day_increment as dt from dual connect by level <= ( ( ( end_date - start_date ) + day_increment ) / day_increment )

  • Statement 19

    Generate every other Monday

    select * from date_mgr.generate_days ( next_day ( date'2021-01-01' - 1, 'Monday' ), date'2021-12-31', 14 )

    DT
    04-JAN-2118-JAN-2101-FEB-2115-FEB-2101-MAR-2115-MAR-2129-MAR-2112-APR-2126-APR-2110-MAY-2124-MAY-2107-JUN-2121-JUN-2105-JUL-2119-JUL-2102-AUG-2116-AUG-2130-AUG-2113-SEP-2127-SEP-2111-OCT-2125-OCT-2108-NOV-2122-NOV-2106-DEC-2120-DEC-21

    26 rows selected.
    select start_date + ( level - 1 ) * day_increment as dt from dual connect by level <= ( ( ( end_date - start_date ) + day_increment ) / day_increment )

  • Statement 20

    Generate months using SQL macro

    select * from date_mgr.generate_months ( next_day ( date'2021-01-01' - 1, 'Monday' ), date'2021-12-31' )

    DT
    04-JAN-2104-FEB-2104-MAR-2104-APR-2104-MAY-2104-JUN-2104-JUL-2104-AUG-2104-SEP-2104-OCT-2104-NOV-2104-DEC-21

    12 rows selected.
    select add_months ( start_date, ( level - 1 ) * month_increment ) as dt from dual connect by level <= ( months_between ( end_date, start_date ) + month_increment ) / month_increment

  • Statement 21

    Using the SQL macro, you can create a row per quarter by setting the increment to 3.

    Generate quarters

    select * from date_mgr.generate_months ( date'2021-01-01', date'2021-12-31', 3 )

    DT
    01-JAN-2101-APR-2101-JUL-2101-OCT-21

    4 rows selected.
    select add_months ( start_date, ( level - 1 ) * month_increment ) as dt from dual connect by level <= ( months_between ( end_date, start_date ) + month_increment ) / month_increment

  • Statement 22

    Generate three years with SQL macros

    select * from date_mgr.generate_years ( date'2020-01-01', date'2022-12-31' )

    DT
    01-JAN-2001-JAN-2101-JAN-22

    3 rows selected.
    select start_date + numtoyminterval ( ( level - 1 ) * year_increment, 'year' ) as dt from dual connect by level <= ( ( months_between ( end_date, start_date ) / 12 ) + year_increment ) / year_increment

  • Statement 23

    with stor as ( select store_id, order_datetime from co.orders o where store_id = 23 ), rws as ( select min ( trunc ( order_datetime ) ) mn_dt, max ( trunc ( order_datetime ) ) mx_dt from stor ) select dt, count ( order_datetime ) from ( select * from rws cross join date_mgr.generate_days ( mn_dt, mx_dt ) ) left join stor on dt <= order_datetime and dt + 1 > order_datetime group by dt order by dt

    DTCOUNT(ORDER_DATETIME)
    22-MAR-19123-MAR-19024-MAR-19225-MAR-19226-MAR-19127-MAR-19228-MAR-19129-MAR-19030-MAR-19031-MAR-19201-APR-19302-APR-19103-APR-19204-APR-19005-APR-19106-APR-19007-APR-19008-APR-19309-APR-19010-APR-19311-APR-19312-APR-191

    22 rows selected.
    select start_date + ( level - 1 ) * day_increment as dt from dual connect by level <= ( ( ( end_date - start_date ) + day_increment ) / day_increment )

  • Statement 24

    Generating dates is a handy trick, but storing the dates in a table makes queries easier to write. It also allows you to business information about dates, such as whether they are working days.You can also add columns for date attributes you want to filter on, such as day-of-week, month-of-year, etc.

    Create a dates table

    create table calendar_dates ( calendar_date date check ( calendar_date = trunc ( calendar_date ) ) not null primary key, is_working_day integer check ( is_working_day in ( 0, 1 ) ) not null, day_of_week varchar2(10 char) ) organization index

    Table created.

  • Statement 25

    This loads the dates table with a row per day, using the SQL macro. While doing so it sets Saturday and Sunday to be non-working days

    Store days of the year

    insert into calendar_dates ( calendar_date, is_working_day, day_of_week ) select dt, case when to_char ( dt, 'dy', 'nls_date_language = english' ) in ( 'sat', 'sun' ) then 0 else 1 end, to_char ( dt, 'FMDay', 'nls_date_language = english' ) from date_mgr.generate_days ( date'2021-01-01', date'2021-12-31', 1 )

    365 row(s) inserted.
    select start_date + ( level - 1 ) * day_increment as dt from dual connect by level <= ( ( ( end_date - start_date ) + day_increment ) / day_increment )
    select start_date + ( level - 1 ) * day_increment as dt from dual connect by level <= ( ( ( end_date - start_date ) + day_increment ) / day_increment )

  • Statement 26

    Set UK public holidays to be non-working days

    update calendar_dates set is_working_day = 0 where calendar_date in ( date'2021-01-01', date'2021-04-02', date'2021-04-05', date'2021-05-03', date'2021-05-31', date'2021-08-30', date'2021-12-27', date'2021-12-28' )

    8 row(s) updated.

  • Statement 27

    commit

    Statement processed.

  • Statement 28

    Find all the Mondays in the year

    select calendar_date from calendar_dates where day_of_week = 'Monday' and calendar_date between date'2021-01-01' and date'2021-12-31'

    CALENDAR_DATE
    04-JAN-2111-JAN-2118-JAN-2125-JAN-2101-FEB-2108-FEB-2115-FEB-2122-FEB-2101-MAR-2108-MAR-2115-MAR-2122-MAR-2129-MAR-2105-APR-2112-APR-2119-APR-2126-APR-2103-MAY-2110-MAY-2117-MAY-2124-MAY-2131-MAY-2107-JUN-2114-JUN-2121-JUN-2128-JUN-2105-JUL-2112-JUL-2119-JUL-2126-JUL-2102-AUG-2109-AUG-2116-AUG-2123-AUG-2130-AUG-2106-SEP-2113-SEP-2120-SEP-2127-SEP-2104-OCT-2111-OCT-2118-OCT-2125-OCT-2101-NOV-2108-NOV-2115-NOV-2122-NOV-2129-NOV-2106-DEC-2113-DEC-21

    Rows 1 - 50. More rows exist.

  • Statement 29

    With the data stored in a table, it's easy to write queries to find working days.

    Find working Mondays in year

    select calendar_date from calendar_dates where day_of_week = 'Monday' and calendar_date between date'2021-01-01' and date'2021-12-31' and is_working_day = 1

    CALENDAR_DATE
    04-JAN-2111-JAN-2118-JAN-2125-JAN-2101-FEB-2108-FEB-2115-FEB-2122-FEB-2101-MAR-2108-MAR-2115-MAR-2122-MAR-2129-MAR-2112-APR-2119-APR-2126-APR-2110-MAY-2117-MAY-2124-MAY-2107-JUN-2114-JUN-2121-JUN-2128-JUN-2105-JUL-2112-JUL-2119-JUL-2126-JUL-2102-AUG-2109-AUG-2116-AUG-2123-AUG-2106-SEP-2113-SEP-2120-SEP-2127-SEP-2104-OCT-2111-OCT-2118-OCT-2125-OCT-2101-NOV-2108-NOV-2115-NOV-2122-NOV-2129-NOV-2106-DEC-2113-DEC-2120-DEC-21

    47 rows selected.

  • Database on OTN SQL and PL/SQL Discussion forums
    Oracle Database
    Download Oracle Database

FAQs

How to calculate years months and days between two dates in Oracle? ›

How to generate days, weeks, or months between two dates in Oracle Database
  1. Subtracting the first date from the last to get the number of days.
  2. Generate this many rows (adding one if you want to include the end date in the output)
  3. Add the current row number (minus one) to the start date.
Jun 1, 2021

How to calculate days between dates in SQL Oracle? ›

When you subtract one date from another in Oracle Database the result is the number of days between them. To see this as the number of days, hours, minutes, and seconds pass this to numtodstinterval with the units day . This returns a day to second interval , which is in the format DD HH24:MI:SS : This format is fixed.

How to get all the months between two dates in Oracle? ›

Oracle Date/Time MONTHS_BETWEEN() Function
  1. Syntax. MONTHS_BETWEEN(date1, date2) ...
  2. Parameters. date1 : first date for calculating the difference. ...
  3. Return. It returns the numeric value.
  4. Example 1. select MONTHS_BETWEEN('20-nov-2018','20-feb-2019') from dual; ...
  5. Example 2. select MONTHS_BETWEEN('20-jun-2019','20-feb-2019') from dual;

How to extract day and month from date in Oracle? ›

You can extract YEAR, MONTH, DAY from a DATE value by using the EXTRACT() function. The following example extracts the value of the YEAR field from a DATE value. In this example, we used the TO_DATE() function to convert a date literal to a DATE value.

How to find difference between two dates in years months and days in SQL? ›

To find the difference between dates, use the DATEDIFF(datepart, startdate, enddate) function. The datepart argument defines the part of the date/datetime in which you'd like to express the difference. Its value can be year , quarter , month , day , minute , etc.

How do I calculate days months and years in SQL? ›

Calculate Age in SQL Server with Years, Months, and Days
  1. CREATE function dbo.Age.
  2. (@dob datetime)
  3. returns varchar(50)
  4. as.
  5. BEGIN.
  6. DECLARE @date datetime, @tmpdate datetime, @years int, @months int, @days int.
  7. DECLARE @Age varchar(50)
  8. set @Age=''
Jul 13, 2015

How to get days between two dates in SQL? ›

MySQL DATEDIFF() Function

The DATEDIFF() function returns the number of days between two date values.

How to get days from date range in SQL? ›

Use the DATEDIFF() function to retrieve the number of days between two dates in a MySQL database. This function takes two arguments: The end date. (In our example, it's the expiration_date column.)

How do you calculate how many days between two dates in SQL? ›

If you wanted to find the number of days between the start and end dates of the Party event, you would use the following query: SELECT DATEDIFF(end_date, start_date, 'day') FROM events WHERE event_name = 'Party';

How do I get a list of months between two dates? ›

Using the Excel DATEDIF Formula
  1. =DATEDIF( start_date, end_date, “M”)
  2. =DATEDIF(B2,C2,"M")
  3. =(YEAR(end_date)-YEAR(start_date))*12+MONTH(end_date)-MONTH(start_date)
  4. =(YEAR(C2)-YEAR(B2))*12+MONTH(C2)-MONTH(B2)
  5. =YEARFRAC( start_date, end_date) * 12.
  6. =YEARFRAC(B2,C2)*12.

How do I get months between two dates in SQL? ›

Returns the number of months between two DATE or TIMESTAMP values. For example, MONTHS_BETWEEN('2020-02-01'::DATE, '2020-01-01'::DATE) returns 1.0.

How to extract months from date in Oracle SQL? ›

To group data by month in Oracle, use the EXTRACT() function. It extracts the given part (year, month, etc.) from the date or timestamp. We use the EXTRACT() function twice: once with the MONTH argument and once with the YEAR argument.

How to extract year and month from datetime? ›

Use pandas DatetimeIndex() to Extract Month and Year

Also, to extract the month and year from the pandas Datetime column, use DatetimeIndex. month attribute to find the month and use DatetimeIndex. year attribute to find the year present in the date. Note that this method takes a date as an argument.

How to calculate days in months in Oracle? ›

The LAST_DAY() function returns the last day of the month based on the specified date. By passing this to TO_CHAR() with a format of DD for the day of the month, we get the number of the last day of the month. That number represents the number of days in the month.

How do you calculate days and years between two dates? ›

To calculate the number of days between two dates, you need to subtract the start date from the end date. If this crosses several years, you should calculate the number of full years. For the period left over, work out the number of months.

How do you calculate the number of months between two dates and years? ›

Using the Excel DATEDIF Formula
  1. =DATEDIF( start_date, end_date, “M”)
  2. =DATEDIF(B2,C2,"M")
  3. =(YEAR(end_date)-YEAR(start_date))*12+MONTH(end_date)-MONTH(start_date)
  4. =(YEAR(C2)-YEAR(B2))*12+MONTH(C2)-MONTH(B2)
  5. =YEARFRAC( start_date, end_date) * 12.
  6. =YEARFRAC(B2,C2)*12.

What is the formula to calculate number of months and days between two dates? ›

Calculate elapsed days/month/years

To calculate elapsed days is so easy, you just need to apply this formula = B2-A2, A2 is the start date, B2 is the end date. Tip: To calculate elapsed month, you can use this formula =DATEDIF(A2,B2,"m"), A2 is the start date, B2 is the end date.

Top Articles
Latest Posts
Article information

Author: Nicola Considine CPA

Last Updated: 27/10/2023

Views: 6360

Rating: 4.9 / 5 (69 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Nicola Considine CPA

Birthday: 1993-02-26

Address: 3809 Clinton Inlet, East Aleisha, UT 46318-2392

Phone: +2681424145499

Job: Government Technician

Hobby: Calligraphy, Lego building, Worldbuilding, Shooting, Bird watching, Shopping, Cooking

Introduction: My name is Nicola Considine CPA, I am a determined, witty, powerful, brainy, open, smiling, proud person who loves writing and wants to share my knowledge and understanding with you.