A GEDCOM-X Date Library for Javascript, fully compliant with the spec.
var GedcomXDate = require('gedcomx-date');
var single = new GedcomXDate('+1900-01-01');
console.log(single.getYear());
// 1900
console.log(single.getHours());
// undefined
var range = new GedcomXDate('A-0100-01-01/P2Y');
var duration = range.getDuration();
var newSingle = GedcomX.addDuration(single, duration);
console.log(newSingle.toFormalString());
// +1902-01-01
var recurring = new GedcomXDate('R10/+1950-01-01/P10Y');
var futureDate = recurring.getNth(5);
console.log(futureDate.toformalString());
// +2000-01-01
var newDuration = GedcomXDate.getDuration(single, new GedcomXDate('+2014-03-01'));
console.log(newDuration.toFormalString());
// P114Y2M
var halfDuration = GedcomXDate.multiplyDuration(newDuration, .5);
console.log(newDuration.toFormalString());
// P57Y1M
You can install GedcomX-Date by cloning this repository or by using npm.
npm install gedcomx-date
Download GedcomXDate.js and enjoy. (Packaged with love by browserify)
There is a very comprehensive test suite.
# Run tests
npm test
# Generate code coverage
npm run coverage
When you create a new GedcomXDate you pass in a formal date string into the contructor. It will parse and validate the string, and return an object representation of it. If there is a parsing error GedcomXDate with throw an error.
var date = new GedcomXDate('A+2000-01-01');
// date will be a Single Date
Returns the string
'single'.
Returns a boolean
as to whether or not the date is approximate.
Returns the year as a number
or undefined
.
Returns the month as a number
or undefined
.
Returns the day as a number
or undefined
.
Returns the hours as a number
or undefined
.
Returns the minutes as a number
or undefined
.
Returns the seconds as a number
or undefined
.
Returns the timezone offset hours as a number
or undefined
.
Returns the timezone offset minutes as a number
or undefined
.
Returns the formal GedcomX representation as a string
.
A range has three components, start, end, and duration
var date = GedcomXDate('A+1000-01-01/+2000-12-31');
// date.start will be a simple date
// date.end will be a simple date
// date.duration will be a duration
Returns the string
'range'.
Returns a boolean
as to whether or not the date is approximate.
Returns a Single or undefined
. Also accessible via the attribute start
.
Returns a Duration or undefined
. Also accessible via the attribute duration
.
Returns a Single or undefined
. Also accessible via the attribute end
.
Returns the formal GedcomX representation as a string
.
A Recurring date is the same as a Range with a few more methods.
var date = GedcomXDate('R2/+1000-01-01/+2000-12-31');
Returns the string
'recurring'.
Returns a boolean
as to whether or not the date is approximate.
Returns the number
of times this date recurs, or javascript Infinity
. Also accessible via the attribute count
.
(Overrides Range.getEnd)
Returns the last instance of the recurring date or undefined
.
Returns the Nth occurence of this Date
Returns the formal GedcomX representation as a string
.
Represents a duration of time.
var date = GedcomXDate('A+1000-01-01/P100Y');
var duration = date.getDuration();
Returns the string
'duration'.
Returns the boolean
false, as a duration is never approximate according to the spec.
Returns the years as a number
or undefined
.
Returns the months as a number
or undefined
.
Returns the days as a number
or undefined
.
Returns the hours as a number
or undefined
.
Returns the minutes as a number
or undefined
.
Returns the seconds as a number
or undefined
.
Returns the formal GedcomX representation as a string
.
There is an attribute and a few convenience functions exposed through GedcomXDate.
Will be a string set to the version of GedcomXDate.
Returns the Duration between the startDate and endDate, or throws an error if startDate >= endDate.
Returns the number of days in the month for the given year.
Adds duration to date and returns a new Single date.
Multiplies a duration by a positive number and returns a new Duration.
Returns a Simple date representation of the given JavaScript Date.
Returns a Simple date representing the current date and time.
Compare two dates. Only works for single dates of the same specificity. Dates may either be in string format or already be Single date objects.
This function is designed to be used as the custom compare function for Array.sort(). Returns:
- 0 if the dates are equal
- -1 if date1 occurs before date2
- 1 if date1 occurs after date2
// Dates as strings
GedcomXDate.compare('+1845-11-13', '+1834-02-17');
// Dates as objects
var date1 = new GedcomXDate('+1394-07-19'),
date2 = new GedcomXDate('+1723-11-04');
GedcomXDate.compare(date1, date2);
// Mix and match
GedcomXDate.compare('+1845-11-13', new GedcomXDate('+1834-02-17'));
// The following will throw errors because the dates do not
// have the same specificity.
GedcomXDate.compare('+1845', '+1834-02-17');
GedcomXDate.compare('+1845-11-13', '+1834-02-17T05:23:45');