# Macro Library dates

A library of Date Functions. Version 1. Dec 17, 2020
Author: Daniel Brown: CU Boulder - Leeds School of Business

The following library allows you to manipulate dates in your question. It is often desireable to have questions that use the current date and then some days or months into the future.

It takes advantage of PHP date formatting. A reference to the available formatting codes can be found here. A few useful formats include 'd-M-Y' for dates like 4-Mar-2020 or 'F j, Y' for dates like March 4, 2020. The date format is optional and if left out will default to 'F j, Y'. The intention is that you will select a preferred date format and then pass that consistently to all functions. Finally, the function dates_dateformat can be used to get useful information from the date. For example, $today=today('d-M-Y') can be used to retreive today's date. $weekday = dates_dateformat($today,"l") will retrieve the day of the week. $month = dates_dateformat($today,"F") will retrive the name of the month.

The question 601187 tests this macro library. If you make changes to the library please modify the test question to incorporate your changes and run to make sure that nothing is broken.

- dates_adddays
- dates_addweeks
- dates_addmonths
- dates_addyears
- dates_bomonth
- dates_eomonth
- dates_diffdays
- dates_randdate
- dates_dateformat

#### dates_adddays

function dates_adddays(date,days,format)

Adds days to the given date. Input and output dates are formatted with the given format.

#### dates_addweeks

function dates_addmonths(date,weeks,format)

Adds weeks to the given date. Input and output dates are formatted with the given format.

#### dates_addmonths

function dates_addmonths(date,months,format)

Adds months to the given date. Input and output dates are formatted with the given format.

#### dates_addyears

function dates_addyears(date,years,format)

Adds years to the given date. Input and output dates are formatted with the given format.

#### dates_bomonth

function dates_bomonth(date,format)

Returns the first day of the month for the given date. Input and output dates are formatted with the given format.

#### dates_eomonth

function dates_eomonth(date,format)

Returns the last day of the month for the given date. Input and output dates are formatted with the given format.

#### dates_diffdays

function dates_diffdays(date1,date2,format)

Returns the number of days between date1 and date2. If date1 is after date2 then this will return a negative number. Input dates are formatted with the given format. This function will truncate the time from the dates when taking the difference. For example, today at 23:30 is one day from tomorrow at 01:30.

#### dates_randdate

function dates_randdate(date1,date2,format)

Returns a random date between date1 and date2. Input dates are formatted with the given format.

#### dates_dateformat

function dates_dateformat(date1,origFormat,newFormat)

Returns a date formatted with a new format. A reference to the available formatting codes can be found here.