dateYMD (int nDate, int &year, int &month, int &day, calendar dateSystem=cal_Gregorian)
Converts a serial Julian date into day, month and year values
void
dateYMD (const char* date, int &year, int &month, int &day, calendar dateSystem=cal_Gregorian, int century21st=30)
Extracts the components of a date (year, month, day) from a text string
Use the following HTML code to embed the calculators within other websites:
DateYMD
voiddateYMD(
int
nDate
int&
year
int&
month
int&
day
calendar
dateSystem = cal_Gregorian
)
This function converts a serial Julian date, into a year, month and day, thereby performing the inverse of date. For the Gregorian calendar this conversion is achieved using:
#include <stdio.h>#include <codecogs/units/date/dateymd.h>usingnamespace Units::Date;
int main(){int d,m,y;
dateYMD(36922, y, m, d);
printf("\n The Gregorian Date is: day=%d month=%d year=%d", d, m, y); // 6 May 1987
dateYMD(36922, y, m, d, cal_Julian);
printf("\n The Julian Date is: day=%d month=%d year=%d", d, m, y); // 23 April 1987return0;
}
Gregorian (default)
Julian
cal_Excel - same as Gregorian, we can see little benefit of subtracting 1900 from the year (easily done yourself!)
References
Tondering, C. 2003. http://www.tondering.dk/claus/cal/node3.html
Parameters
nDate
is the number of days from 24 November 4714 BC - otherwise known as the Julian Period (also 1 January 4713BC in the Julian Calendar).
year
is a location (passed by reference) into which is placed the Year.
month
is a location (passed by reference) into which is placed the Month.
day
is a location (passed by reference) into which is placed the Day.
dateSystem
selects which date system the result will be converted into (see constants):
This function uses a number of rules to work out the most likely date, returning the individual elements (Year, Month and Day) of the date. Unlike the Excel version this function is extremely forgiving, it will therefore interpret most date forms and will not return an error.
Todo
Anyone wanting a challenge can try optimising this function and including an option to allow alternative defaults.
Example 2
This example is intended to illustrates the type of dates that can be processed, rather than to being functional (see units/date for a further examples).
#include <stdio.h>#include <codecogs/units/date/date.h>usingnamespace Units::Date;
void main(){int d,m,y;
dateYMD("3245/4 20", y,m,d, cal_Excel); // 20 April 3245
dateYMD("4February 4", y,m,d); // 4 February 0004
dateYMD("40 mar 1664", y,m,d); // 40 March 1664 !!!
dateYMD("24-10/40", y,m,d, cal_Excel); // 24 October 1940
dateYMD("12@31@4", y,m,d); // 12 April 0031
dateYMD("16 5 march", y,m,d, cal_Excel); // 16 March 2005
dateYMD("jansomerubbish4 10", y,m,d, cal_Excel, 10); // 4 January 2010}
Any separator that isn't numeric or alphabetic can be used (i.e. non alphanumeric). So you can not have negative years, because '-' is often used as a separator.
Only the first 3 terms will be processed. If only 2 terms are specified, today's year is added to the end.
All things being equal the default date order is: day, month, year. However:
if the Year is definitely first, the default becomes year, month, day.
if the Month is definitely first, the default becomes month, day, year.
Any number>31 is assumed to be year. So "1985/3/4' is the 4 March 1985 (N.B. in this example date order is now year/month/day)
Numbers less than or equal to 12 get a higher 'month' weighting, so "3/30/12" will be assumed to the 3 December 1930.
If the month is obvious then the function checks the remaining numbers against possible length for that month. So "31 February 2" is 2 February 1931, because February doesn't have 31 days. However, "31 February 2002" will return 31 February 2002, because neither 31 nor 2002 are valid days for February, so we switch to the default format of day/month/year. Equally "20 March 3", is 20 March 2003.
Only the first 3 characters of a text month description are used, the rest is ignored.
Note
This function can extract an illegal date, e.g. dateYMD("31 February 2004",y,m,d) will return the 31st February 2004. To be confident of having a legitimate date, use date instead, e.g. date("31 February 2004") will return the 2 March 2004.
Parameters
date
is a text string containing a date, with the following attributes:
year
is a location (passed by reference) into which is placed the Year.
month
is a location (passed by reference) into which is placed the Month.
day
is a location (passed by reference) into which is placed the Day.
dateSystem
only has an effect if equal to cal_Excel, when it adds 1900 to any dates below 1900 (see constants).
century21st
defines the two digit years that occurs in the 21st century, as opposed to the 20th Century. In any DateSystem not equal to cal_Excel this parameter is ignored, otherwise the 2000 is added to any year values below Century21st, while 1900 is added to years equal to and above Century21st (Within cal_Excel this variable is set once for all sheets via the options panel).