Template talk:YMD2MJD
Documentation[edit]
{{YMD2MJD}}
converts a calendar date given as yyyy mm dd (year, month, day) to a Modified Julian Date. MJD needs only five decimal digits for dates in this and the previous century, JD needs seven.
The remaining numerical precision (after the decimal point) is available for timestamps. As shown for MJD2sec the 12 available digits for #expr: output allow unfortunately only 1/100 seconds for current dates.
Dates starting with 02000301 in the Common Era before the introduction of the Gregorian calendar 15821015 (Rome), 17520914 (UK), etc. use the proleptic Gregorian calendar.
Dates with null year (1 BC) or negative years (2 BC, and earlier) are handled as in the w:en:proleptic Julian calendar (the Julian calendar with Augustus rules on leap years was actually not applied before 8 AD, the initial Caesar's Julian calendar starting sometime after 45 BC using different triennial lead year rules suspended for 12 years by Augustus, so all years before 8 AD are also handled as in the proleptic Julian calendar), with astronomical year numbering (ISO year 0 is 1 BC, ISO year 1 is 2 BC, and so on). "Negative" dates are rarely used, but if they are used they are generally Julian dates. One famous example is 1 January 4713 BC (ISO year 4712) at noon, the conventional start of the JD epoch.
From 02000301 12:00:00 to 03000228 23:59:59, the Julian calendar and the proleptic Gregorian calendar both agree. So this template uses a smooth transition from Julian to proleptic Gregorian calendar dates at an arbitrary date anywhere within this third century, instead of using the legal date of change from the Julian calendar to the Gregorian calendar a dozen of centuries later (or more, because the effective date of adoption depends on countries or cultures) with an abrupt shift of dates (e.g. 10 days were skipped from the calendar when the Gregorian calendar was first adopted in 1582 or in the 17th century, 11 days were skipped in the 18th century, 12 days in the 19th century, 13 days in the 20th and 21st centuries).
Please note that there are already proposals for another calendar reform in the year 2800 or 4000, not only proleptic dates are dubious. The advantage of counting seconds or days is to avoid such transition problems.
Julian dates (JD) also count days, the difference between modified Julian dates (MJD) and JD is constant:
 MJD = JD  2400000.5, JD starts at 12:00 UTC
 JD = MJD + 2400000.5, MJD starts at 00:00 UTC
This template does not count days starting at noon conventionally in the Julian calendar, but still count them starting at midnight. In the old Roman calendars, times in the morning were considered part of the previous day, but were still reported in reference to the next day with some count of hours before noon on thar next day.
For example, the Julian date "17 August 80, 03:00 before noon" in the morning was formally part of the 16 August day and should noted numerically as "00800817 03:00 JD", or "00800817 03:00bn JD", or "00800816 09:00am JD" (modern 12hour clock convention), but not "00800816 09:00 JD" (ambiguous 12hour clock) or "00800816 21:00 JD" (modern 24hour clock) which both remain ambiguous. So, this template still uses the hour=09 parameter value, not the hour=03 parameter value (which would be computed as 3 hours before the start of the day at midnight), and will interpret the parameter hour=12 as meaning noon (in the 24hour clock convention).
This practice for noting time of day before and after noon disappeared when the day was officially divided in 24 equal hours (including the night hours) starting at midnight and always counted positively, instead of a single clock with hours of variable lengths (depending on seasons), counted positively or negatively, and only when the Sun was visible (and where nightly hours not counted, or just reported counting the whole night at the same date from the previous noon). In these ancient period, accurate clocks still did not exist, so time was very imprecise and most documents do not specify it but just indicate the same date for the morning and afternoon and the modern nightly time "02:00am" ("02:00" since midnight in 24hour clock notation) would be 10 hours before noon in old texts, if it was measurable.
Note also that this template also uses a single timezone, UTC time, without daylight saving. To convert a local time, just add the local timezone offset to the numeric value of the hour parameter.
Usage[edit]
{{YMD2MJD}}
converts the current date: , when no year parameter is specified.{{YMD2MJDyyyy}}
converts the date on 1 January in the specified year, at noon.{{YMD2MJDyyyymmdd}}
converts a given date, at noon, see below for details.{{YMD2MJDyyyymmddhhMMss}}
converts a given date, at specified time, see below for details. {{#expr: (3+
{{YMD2MJD}}
) mod 7}} = 2, {{CURRENTDOW}} = 2
{{YMD2MJD18581117}}
= 0, begin of MJD epoch{{YMD2MJD19950927}}
= 49987, USNO test date (49987)
 All years before the 3rd century are interpreted in the Julian calendar (years before Christian era are passed negatively — up to yyyy=0000, which means 1 BC, the year immediately before 1 AD).
 All years after the 3rd century are interpreted in the Gregorian calendar (or proleptic Gregorian calendar).
 Years in the 3rd century (from 0201 to 0300) are interpreted identically in both calendars.
Test suite[edit]
Below you find a part of a ported REXX MJD test suite, it includes 19700101, the base of POSIX timestamps (counting seconds instead of days). It also contains some obscure dates where the author ran into debugging problems. The test dates for 1895, 1896, 1900, and 2000 are adapted from JD.
Code  Result  MJD  JD  REXX  Note 

{{YMD2MJD47131230120000}} =

2400000.5  ?  ?  ?  Start of proleptic Julian calendar? 
{{YMD2MJD00440228}} =

694954  694953  1705047.5  16378  End of proleptic Julian calendar 
{{YMD2MJD00440301}} =

694952  694952  1705048.5  16377  Start of Julian calendar 
{{YMD2MJD00000301}} =

678881  678881  1721119.5  306  
{{YMD2MJD00010101}} =

678575  678575  1721425.5  0  
{{YMD2MJD00010131}} =

678545  678545  1721455.5  30  
{{YMD2MJD00010201}} =

678544  678544  1721456.5  31  
{{YMD2MJD00010228}} =

678517  678517  1721483.5  58  
{{YMD2MJD00010301}} =

678516  678516  1721484.5  59  
{{YMD2MJD00010831}} =

678333  678333  1721667.5  242  
{{YMD2MJD00010901}} =

678332  678332  1721668.5  243  
{{YMD2MJD00040229}} =

677421  677421  1722579.5  1154  
{{YMD2MJD00040301}} =

677420  677420  1722580.5  1155  
{{YMD2MJD03000228}} =

569310  569299  1830701.5  109276  (actual Julian date: 03000229) 
{{YMD2MJD03000301}} =

569309  569298  1830702.5  109277  Start of the proleptic Gregorian calendar (for the smooth transition; identical Julian date) 
{{YMD2MJD04000228}} =

532786  569299  1830701.5  109276  End of the Julian calendar (for the smooth transition; identical Gregorian date) 
{{YMD2MJD04000301}} =

532784  569298  1830702.5  109277  (equivalent Julian date: 04000229) 
{{YMD2MJD12590302}} =

219040  219040  2180960.5  459535  
{{YMD2MJD15161230}} =

124870  124870  2275130.5  553705  
{{YMD2MJD15821014}} =

100841  100841  2299159.5  ?  End of the proleptic Gregorian calendar (actual Julian date: 15821004) 
{{YMD2MJD15821015}} =

100840  100840  2299160.5  577735  Start of the Gregorian calendar (equivalent Julian date: 15821005, where Gregorian calendar is not applied) 
{{YMD2MJD17520914}} =

38779  38779  2361221.5  639796  
{{YMD2MJD18581117}} =

0  0  2400000.5  678575  
{{YMD2MJD18950228}} =

13252  13252  2413252.5  691827  
{{YMD2MJD18950301}} =

13253  13253  2413253.5  691828  
{{YMD2MJD18960229}} =

13618  13618  2413618.5  692193  
{{YMD2MJD18960301}} =

13619  13619  2413619.5  692194  
{{YMD2MJD19000228}} =

15078  15078  2415078.5  693653  
{{YMD2MJD19000301}} =

15079  15079  2415079.5  693654  
{{YMD2MJD19700101}} =

40587  40587  2440587.5  719162  
{{YMD2MJD19770101}} =

43144  43144  2443144.5  721719  
{{YMD2MJD20000229}} =

51603  51603  2451603.5  730178  
{{YMD2MJD20000301}} =

51604  51604  2451604.5  730179  
{{YMD2MJD27381128}} =

321424  321424  2721424.5  999999 
Input ranges[edit]
Interpretation of input parameters out of standard ranges: enabled
{{YMD2MJD2000.511}}
= 51726 // denormalized year with fraction (halfyear){{YMD2MJD20000701}}
= 51726 // year normalized by adding 6 months
{{YMD2MJD20001301}}
= 51910 // unnormalized month (>12){{YMD2MJD20010101}}
= 51910 // month normalized to next year
{{YMD2MJD20000031}}
= 51543 // unnormalized month (<1){{YMD2MJD2000101}}
= 51483 // month normalized to previous year
{{YMD2MJD20000100}}
= 51543 // unnormalized day (<1){{YMD2MJD19991231}}
= 51543 // day normalized to previous month
{{YMD2MJD20000132}}
= 51575 // unnormalized day (>31 in January){{YMD2MJD20000201}}
= 51575 // day unnormalized to next month
{{YMD2MJD200011.5}}
= 51544.5 // fraction of day (half day){{YMD2MJD20001112}}
= 51544.5 // fraction of day normalized as time (noon)
Minimal and maximal date not resulting in exponential notation:
{{YMD2MJD2737848929918}}
= 999999999998{{YMD2MJD2737848929919}}
= 999999999997{{YMD2MJD27379088651111}}
= 999999999999{{YMD2MJD27379088651112}}
= 1000000000000
Smooth proleptic Gregorian to Julian calendar date transition:
{{YMD2MJD47120101}}
= 2399999, proleptic Julian calendar.{{MJD4712010112}}
= 2399998.5, Julian Day 0 at noon.
{{YMD2MJD47120102}}
= 2399998, JD = +0.5 (4713 BC).
Start of 1st century (on 00010101 BC (Anno Domini) in Julian calendar, a leap year
{{YMD2MJD00000101}}
= 678941, 1st day March 1 BC (Start of Julian CE) ...
{{YMD2MJD00001231}}
= 678576, 1st day March 1 BC (Start of Julian CE)
Start of 1st century (on 00010101 CE in Julian calendar, NOT a leap year
{{YMD2MJD00010101}}
= 678516, 1st day March 1 (Start of Julian CE){{YMD2MJD00010228}}
= 678516, 1st day March 1 (Start of Roman CE)
{{YMD2MJD00010301}}
= 678516, 1st day March 1 (Start of Roman CE) ...
{{YMD2MJD00991231}}
= 642417, 1st day March 1 (Start of Roman CE)
{{YMD2MJD01000101}}
= 642416, 1st day January 100 ...
{{YMD2MJD01000228}}
= 642358, last day February 100 (Gregorian){{YMD2MJD01000229}}
= 642357, last day February 100 (Julian)
End of 1st century (on 01000229 in Julian calendar, a leap year, but NOT leap in Gregorian poleptic calendar)
Start of 2rd century (on 01000301 in Julian calendar, a leap year, but NOT leap in Gregorian poleptic calendar):
{{YMD2MJD01000301}}
= 642356{{YMD2MJD01000302}}
= 642355,{{MJD2D642357}}
= 1 (Gregorian proleptic) ...
{{YMD2MJD01991231}}
= 605892, last day December 199
{{YMD2MJD02000101}}
= 605891, 1st day January 200 ...
{{YMD2MJD02000227}}
= 605834,{{MJD2D605836}}
= 26 (Gregorian proleptic){{YMD2MJD02000228}}
= 605833, last day February 200 (Gregorian proleptic){{YMD2MJD02000229}}
= 605832, last day February 200 (Julian)
End of 2rd century (on 02000229 in Julian calendar, a leap year, but NOT leap in Gregorian poleptic calendar)
{{YMD2MJD02000230}}
= 605831, next day
Start of 3rd century (on 02000301 in both calendars):
{{YMD2MJD02000301}}
= 605831,{{MJD2D605833}}
= 1 ...
{{YMD2MJD02991231}}
= 569369, last day December 299
{{YMD2MJD03000101}}
= 569368, 1st day January 300 ...
{{YMD2MJD03000228}}
= 569310, last day February 300
End of 3rd century (on 03000229 in Julian calendar, a leap year).
{{YMD2MJD03000229}}
= 569309, 300 was no Gregorian leap year, next day
Maps to start of 4th century (on 03000301 in Gregorian calendar, NOT a leap year): 1st difference
Optional substitution works if all three parameters and subst=subst: are specified, or for the current date {{subst:YMD2MJDsubst=subst:}}.
See also[edit]
 template:YMD2MJD (edit•talk•links•history)
 template:Dectime (edit•talk•links•history)
 template:MJD (edit•talk•links•history)
 template:MJD2D (edit•talk•links•history)
 template:MJD2sec (edit•talk•links•history)
 template:Sec2MJD (edit•talk•links•history)
 template:Timestamp2MJD (edit•talk•links•history)
 template:Gsd (edit•talk•links•history)
 w:Template:Gregorian serial date
Discussion[edit]
 Add issues below as you see fit, sign with ~~~~
Empty output with denormal numbers[edit]
 "0.9900000001  floor(0.9900000001 / 0.03) * 0.03" = "1.0000000827404E10"
 This uses PHP's fmod() function but arguments are left as double, no rounding occurs and precision is kept
 Expected value: 1E10 with 8 digits of precision
 The result is: 1.0000000827404E10 (excellent order of magnitude and precision)
 "0.9900000001 mod 0.03" = "Expression error: Division by zero"
 This uses PHP's fmod() function but arguments are incorrectly downcasted to float, forcing rounding and loss of precision.
 Expected value: 1E10 with 8 digits of precision
 The result was: 4.4408920985006E16 (bad: 6 orders of magnitude lost, out of precision)
 Now this is incorrect (the result is an empty string)
 "(2 * pi)  floor( (2 * pi) / (pi / 6) ) * (pi / 6)" = "0"
 Expected value: 0 with an absolute error not exceeding pi/2*1E14
 Effective result: 0 (excellent, but could be by chance; for other divisors set to (PI/N), where N is an odd prime, the max error of PI/2/N*1E14 is respected)
 "(2 * pi) mod (pi / 6)" = "Expression error: Division by zero"
 Expected value: 0 with an absolute error not exceeding pi/2*1E14
 Effective result was: 1.0000003602961E10 (very bad, the absolute error is too large by a factor of about 1E7)
 Now this is incorrect (the result is an empty string)
So beware of implementations using MOD, floor() is always better. verdy_p (talk) 15:34, 27 December 2012 (UTC)