Converting a civil date to lunar, Tibetan and Hijri dates with tyme4ts
A TypeScript calendar library that converts Gregorian dates to Chinese lunar, Tibetan, and Hijri dates and provides solar terms and moon phases.
At a glance
- What is it?
- A single SolarDay can represent the starting point for several calendar conversions in TypeScript. The project’s examples and tests also pin down less obvious behavior around leap months, day indexes and the October 1582 calendar change.
- Who is it for?
- Use tyme4ts when your application needs calendar-aware dates and arithmetic. Preserve the input calendar and test its conventions against representative records, particularly for a leap month or a date near the October 1582 cutover.
- Can I use it commercially?
- Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Yes. The repository last received commits 15 days ago.
- What is it written in?
- Mainly TypeScript, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on November 25, 2026, and from our analysis. They are not legal advice.
Editorial analysis
One input date, three different questions
Take May 29, 1986. A date picker might store those three numbers, while a family archive might also want the corresponding Chinese lunar date, Tibetan date or Hijri date. Changing the language used to print the month would leave the calendar fields themselves untouched. Tyme4ts supplies day objects for the different calendars and conversion methods between them. That distinction matters when the converted month or year is used for grouping records rather than simply decorating a heading.
The project's example constructs a SolarDay for May 29 and requests the three other representations from that same object. The lunar result falls on the twenty-first day of the fourth month. The Hijri result falls on the twentieth day of Ramadan in year 1406 according to this implementation. Keeping a common source day makes a useful test fixture. If a UI displays a different result for one calendar, the investigation can begin with the conversion and rendering of that one column.
Install the package and preserve the day object
The npm package is named tyme4ts. In a TypeScript application with a working compiler and runner, only the calendar library needs to be added for the README's conversion example. The longer setup sequence in the repository also creates a new npm project and adds TypeScript and ts-node for people starting with an empty directory.
npm i tyme4tsThe public import uses the package name. The manifest supplies a CommonJS entry, an ES module entry and a declaration file for type checking. Consumers do not need to import a test file or a private path under the library's source directory. The following two lines from the example create a date without involving a clock or time-of-day argument:
import {SolarDay} from 'tyme4ts';
const solar: SolarDay = SolarDay.fromYmd(1986, 5, 29);Keep that object while the program needs to derive other dates. Turning it into display text first would leave a later component to parse a localized description just to regain the structured value that was already available.
A lunar month can need more than a number
The conversion example calls getLunarDay on the solar day, then calls toString on the result to display it. Those two operations have different purposes. Conversion chooses a position in another calendar. Formatting chooses a human-readable description of that position. Preserve that distinction in a database import: a display label is useful for readers, while identifying a record from its year, month, day and calendar requires enough detail to distinguish repeated months.
One solar-day test covers May 24, 2020. The expected lunar date explicitly names a leap fourth month. An application that stores just the numeral four loses information that the library displayed. This matters for an anniversary filter as well as a historical record. Decide whether the filter means an ordinary month, a leap month or either before building it from the text shown on a card. Include a leap-month fixture in the application's own tests so a change to its schema cannot silently discard the distinction.
What a round trip checks in another calendar
The Hijri tests include a forward and reverse example for May 13, 2026. They expect year 1447, month eleven, day twenty-six, then construct that Hijri day and convert it back to the original solar date. This pair checks that the two operations agree on a concrete day. It gives a better regression test for a date editor than checking only whether a formatted result contains a particular year.
The RabByung tests use Tibetan cycle, element and zodiac information when constructing a year for a reverse conversion. One case begins with April 23, 2025, identifies a day in the second month of a Wood Snake year and converts the constructed date back to April 23. Names such as Wood Snake recur across cycles. If a source record includes the cycle, keep it with the name. A drop-down that stores the name alone can turn a distinct year into an ambiguous selection even when the library's conversion is behaving exactly as specified.
Counting days across February and October
Day arithmetic exposes assumptions that a modern birthday example will never encounter. The tests give January 1 an index in the year of zero. The final day of 2023 therefore has index 364; the final day of the leap year 2020 has index 365. If a report presents January 1 to readers as day number one, convert the index at the display boundary. Using that displayed ordinal as an index later would shift every row by a day.
The solar-day suite makes an explicit historical choice as well. It expects October 15, 1582 minus October 4, 1582 to be one day. Stepping back one day from the fifteenth returns the fourth. The lunar suite checks corresponding consecutive lunar days against those two solar dates. This model affects interval lengths around the cutover. A catalog that deliberately uses a continuous proleptic Gregorian calendar needs to reconcile that difference before comparing durations. Modern leap-day tests alone will not reveal it.
Choosing which representation to keep
For records that already contain a civil day, store the original year, month and day together with the source calendar when their origin matters. Additional representations can be derived for a view or an export. For records that begin as a timestamp, decide which local civil day the application wants before passing the fields into SolarDay. The constructor used in the example takes three numbers. It has no information from which to infer a user's time zone or decide which side of midnight an event belongs on.
A plain localized date formatter is sufficient when the requirement is only to change the presentation of an existing date. Tyme4ts becomes relevant when the application also needs a calendar-specific object, a reverse conversion or day arithmetic. Start a migration with a round-trip fixture from the relevant test suite and a boundary case from your actual collection. A day that crosses the selected historical cutover or identifies a repeated lunar month tells you much more about compatibility than an ordinary date in the middle of June.
Editorial conclusion
Use tyme4ts when your application needs calendar-aware dates and arithmetic. Preserve the input calendar and test its conventions against representative records, particularly for a leap month or a date near the October 1582 cutover.
Frequently asked questions
How do I get a Chinese lunar date from a SolarDay?
Construct the civil date with SolarDay.fromYmd and call getLunarDay. Use toString on the returned object when you need a display label.
Does tyme4ts count January 1 as index zero?
Yes. Its solar-day tests expect getIndexInYear to return zero for January 1 and 364 for December 31 of 2023.
What does subtracting October 4 from October 15, 1582 return?
The project tests expect a difference of one day across that cutover. Check this convention before comparing historical intervals with a proleptic calendar system.
Official sources
Was this useful?