-
-
Notifications
You must be signed in to change notification settings - Fork 0
N6LDate
NAS6mixfoolv edited this page Aug 17, 2026
·
7 revisions
N6LDate is a high-precision, feature-rich calendar calculation class that extends the standard JavaScript Date object. Key features include:
- Support for calendar changes (Gregorian/Julian) specific to different countries
- Mutual conversion between JD (Julian Day) and MJD
- Mutual conversion with UNIX time (seconds / milliseconds)
- Support for region-specific day-of-week and month names
- Polymorphic constructor (supports 7+ input formats)
-
Calendar normalization via
normalize() - Precision suitable for orbital calculations and astronomical applications
const dt = new N6LDate().normalize();N6LDate uses a two-stage initialization model:
- constructor Sets values ??only (does not guarantee calendar consistency)
- normalize() Handles calendar changes, month-length adjustments, day-of-week calculations, UNIX recalculation, etc.
| Input Format | Example | Description |
|---|---|---|
| No arguments | new N6LDate() |
Current time (Date-compatible) |
| N6LDate instance | new N6LDate(dt) |
Exact copy |
| Date object | new N6LDate(new Date()) |
Convert from Date |
| String | new N6LDate("2030-09-15T12:00:00Z") |
Date.parse() compatible |
| UNIX milliseconds | new N6LDate(Date.now()) |
UNIX ms → JD → Calendar |
| Year, Month, Day | new N6LDate(2030,9,15) |
Set calendar values ??directly |
| Y, M, D, h, m, s, ms | new N6LDate(2030,9,15,12,34,56,789) |
Fractional values ??adjusted via normalize()
|
An essential process to ensure calendar consistency.
- Month length adjustment
- Year length adjustment
- Calendar reform (ReformTable) application
- Day-of-week calculation
- UNIX time recalculation
- JD / MJD recalculation
const dt = new N6LDate(2030,9,15,12).normalize();N6LDate switches the names of days of the week and months based on the region code.
Example:
dt.regionW = "JP"; // Japanese day names
dt.region = "JP"; // Japanese calendar reformSupported regions: JP / EN / FR / DE / ES / IT / PT / RU / GR / … (many others)
Stores the transition dates from the Julian calendar to the Gregorian calendar, which vary by country.
Example (Japan):
1582-10-04 → 1582-10-15 (10-day gap)
Automatically applied during normalize(). ---
const jd = dt.toJD();dt.fromJD(jd).normalize();toUNIX() // UNIX seconds
fromUNIX(sec) // UNIX seconds → Calendar datetoUNIXms() // UNIX milliseconds
fromUNIXms(ms) // UNIX milliseconds → Calendar datestatic get N6LDATE_FMT_DEFAULT() { return 0; } // Local format (Date + Day of Week + Time)
static get N6LDATE_FMT_FULL() { return 1; } // Local format (Date + Time + Day of Week)
static get N6LDATE_FMT_NW() { return 2; } // Local format (Date + Time, no Day of Week)
static get N6LDATE_FMT_DATE() { return 3; } // Local format (Date + Day of Week)
static get N6LDATE_FMT_DATE_NW() { return 4; } // Local format (Date, no Day of Week)
static get N6LDATE_FMT_ISO() { return 5; } // ISO format (YYYY-MM-DD(Day) T hh:mm:ss.SSS Z)
static get N6LDATE_FMT_ISO_W() { return 6; } // ISO format (YYYY-MM-DD T hh:mm:ss.SSS Z + Day of Week)
static get N6LDATE_FMT_ISO_NW() { return 7; } // ISO format (YYYY-MM-DD T hh:mm:ss.SSS Z)
static get N6LDATE_FMT_ISO_D() { return
``` 8; } // ISO format (YYYY-MM-DD + day of week)
static get N6LDATE_FMT_ISO_D_NW() { return 9; } // ISO format (YYYY-MM-DD)dt.toString(N6LDate.N6LDATE_FMT_ISO);var pad = (n, len = 2) => String(n).padStart(len, '0');
const map = {
"YYYY": year,
"YY": String(year).slice(-2),
"MMMM": monthName,
"MMM": monthName.slice(0,3),
"MM": pad(month),
"M": month,
"DD": pad(day),
"D": day,
"hh": pad(hour),
"h": hour,
"HH": pad(hour),
"mm": pad(minute),
"m": minute,
"ss": pad(second),
"s": second,
"SSS": pad(ms,3),
"A": ampm,
"W": weekStr
};dt.format("YYYY-MM-DD hh:mm:ss.SSS (W)")const dt = new N6LDate(2030,9,15,12).normalize();
const target = new N6LDate(2060,9,15,12).normalize();
console.log(dt.toString()); // Local format
console.log(dt.toUNIX()); // UNIX seconds
console.log(dt.toUNIXms()); // UNIX milliseconds
console.log(dt.toJD()); // JD
console.log(dt.toString(N6LDate.N6LDATE_FMT_ISO)); // ISO format
console.log(dt.adjustCalendar(1).toString()); // Adjust by adding 1 day
console.log(dt.addDays(-1.25).toString()); // Add -1.25 days
console.log(dt.diffDays(target)); // Difference in days
//Programmed by NAS6
//date.js
class N6LDate {
//Always append .normalize(), e.g., var dt = new N6LDate().normalize();
constructor(ya, ma, da, ho, mo, so, ms, region = "DEFAULT", regionW = "DEFAULT")
//Clone
clone()
tempPDays(val)
adjustTime(val)
//Get total days (automatic handling of Julian/Gregorian calendars; includes potential errors)
//For accurate elapsed days, retrieve N6LDate.pastday or N6LDate.ms after calling normalize() or adjustCalendar()
calendarToDay(val, flg = false)
//Convert to previous day
suboneday(val)
//Parse date
parseDate(str)
//Get week
getWeek()
//Get total milliseconds / tick time (Epoch: 1/1/1 0:0:0.000)
getMS()
//Get total days (Epoch: 1/1/1 0:0:0.000)
getPDays()
//Get time
getTime()
//Week number
getWeekNumber()
//ISO Week Date (ISO 8601)
getISOWeekDate()
//ISO 8601 Time Duration (duration notation)
getISOTimeDuration(target)
// Calendar reform information
getReformInfo(region = "GB", lng = "DEFAULT")
// Julian/Gregorian calendar reform
afterGregorian(val)
// Check if the current year is a leap year
isLeap(val, flg = true)
// Check if it is the end of the month
isMonthEnd(val)
// Get the end of the month
getMonthEnd(val, flg = true)
// Period count up collection(y,ry,term) // Get cumulative leap years
getLeap(val)
// Includes modification of 'this'
SetVal(val)
// Generate a Gregorian date object from a cumulative day count
gregorianDayToDate(targetDay, flg = true)
// Julian calendar addition (generate a Julian date object from a cumulative day count)
julianDayToDate(targetDay, flg = false)
// Date adjustment (automatic handling for Julian/Gregorian calendars)
adjustCalendar(rh = null)
// [!!!Cumulative milliseconds!!!][Date/Time/Week] Normalization
// Includes modification of 'this'
// Normalize the object's date and time (safe implementation avoiding recursion)
normalize()
// Get difference in milliseconds (this - target)
diffMS(target)
// Get difference in days (this - target)
diffDays(target)
// Get difference structure (this - target)
diffDuration(target)
// Comparison operations
isBefore(target)
isAfter(target)
isEqual(target, eps = 1e-6)
// N6LDate standard format constants
// Used as preset specifiers for toString(fmtsw)
static get N6LDATE_FMT_DEFAULT() { return 0; } // "2026/07/29(Wed) 04:55:04.123"
static get N6LDATE_FMT_FULL() { return 1; } // "2026/07/29 04:55:04.123 (Wed)"
static static get N6LDATE_FMT_NW() { return 2; } // "2026/07/29 04:55:04.123"
static get N6LDATE_FMT_DATE() { return 3; } // "2026/07/29(Wed)"
static get N6LDATE_FMT_DATE_NW() { return 4; } // "2026/07/29"
static get N6LDATE_FMT_ISO() { return 5; } // "2026-07-29(Wed) T 04:55:04.123 Z"
static get N6LDATE_FMT_ISO_W() { return 6; } // "2026-07-29 T 04:55:04.123 Z (Wed)"
static get N6LDATE_FMT_ISO_NW() { return 7; } // "2026-07-29 T 04:55:04.123 Z"
static get N6LDATE_FMT_ISO_D() { return 8; } // "2026-07-29(Wed)"
static get N6LDATE_FMT_ISO_D_NW() { return 9; } // "2026-07-29"
// Formatted output (e.g., "2026/07/29 04:55:04 Wed")
toString(fmtsw = 0)
// Custom format output
// e.g., date.format("YYYY-MM-DD hh:mm:ss.SSS (W)")
format(pattern)
// Date object output
toDate()
// Temporal object output
toTemporal()
// Create with current date and time
Now()
// Julian Day
toJD()
fromJD(rh)
// Modified Julian Day
toMJD()
fromMJD(rh)
// Unix time (seconds)
toUNIX()
fromUNIX(rh)
// Unix time (milliseconds)
toUNIXms()
fromUNIXms(rh)
// Serialization
//const dt = new N6LDate(2030, 9, 15, 12, 34, 56, 789).normalize();
//dt.region = "JP";
//const json = JSON.stringify(dt); // toJSON is called automatically
// → {"year":2030,"month":9,"day":15,...}
//const restored = N6LDate.fromJSON(JSON.parse(json));
//console.log(restored.toString()); // 2030/09/15(Sun) 12:34:56.789
toJSON()
static fromJSON(json)
startOf(unit)
endOf(unit)
// Various addition methods // Simply calls adjustCalendar with the argument treated as elapsed days (including fractions)
addYears(rh)
addMonths(rh)
addDays(rh)
addHours(rh)
addMinutes(rh)
addSeconds(rh)
addMSs(rh)
}
//date.js
class N6LDate {
//Always append .normalize(), e.g., var dt = new N6LDate().normalize();
constructor(ya, ma, da, ho, mo, so, ms, region = "DEFAULT", regionW = "DEFAULT")
//Clone
clone()
tempPDays(val)
adjustTime(val)
//Get total days (automatic handling of Julian/Gregorian calendars; includes potential errors)
//For accurate elapsed days, retrieve N6LDate.pastday or N6LDate.ms after calling normalize() or adjustCalendar()
calendarToDay(val, flg = false)
//Convert to previous day
suboneday(val)
//Parse date
parseDate(str)
//Get week
getWeek()
//Get total milliseconds / tick time (Epoch: 1/1/1 0:0:0.000)
getMS()
//Get total days (Epoch: 1/1/1 0:0:0.000)
getPDays()
//Get time
getTime()
//Week number
getWeekNumber()
//ISO Week Date (ISO 8601)
getISOWeekDate()
//ISO 8601 Time Duration (duration notation)
getISOTimeDuration(target)
// Calendar reform information
getReformInfo(region = "GB", lng = "DEFAULT")
// Julian/Gregorian calendar reform
afterGregorian(val)
// Check if the current year is a leap year
isLeap(val, flg = true)
// Check if it is the end of the month
isMonthEnd(val)
// Get the end of the month
getMonthEnd(val, flg = true)
// Period count up collection(y,ry,term) // Get cumulative leap years
getLeap(val)
// Includes modification of 'this'
SetVal(val)
// Generate a Gregorian date object from a cumulative day count
gregorianDayToDate(targetDay, flg = true)
// Julian calendar addition (generate a Julian date object from a cumulative day count)
julianDayToDate(targetDay, flg = false)
// Date adjustment (automatic handling for Julian/Gregorian calendars)
adjustCalendar(rh = null)
// [!!!Cumulative milliseconds!!!][Date/Time/Week] Normalization
// Includes modification of 'this'
// Normalize the object's date and time (safe implementation avoiding recursion)
normalize()
// Get difference in milliseconds (this - target)
diffMS(target)
// Get difference in days (this - target)
diffDays(target)
// Get difference structure (this - target)
diffDuration(target)
// Comparison operations
isBefore(target)
isAfter(target)
isEqual(target, eps = 1e-6)
// N6LDate standard format constants
// Used as preset specifiers for toString(fmtsw)
static get N6LDATE_FMT_DEFAULT() { return 0; } // "2026/07/29(Wed) 04:55:04.123"
static get N6LDATE_FMT_FULL() { return 1; } // "2026/07/29 04:55:04.123 (Wed)"
static static get N6LDATE_FMT_NW() { return 2; } // "2026/07/29 04:55:04.123"
static get N6LDATE_FMT_DATE() { return 3; } // "2026/07/29(Wed)"
static get N6LDATE_FMT_DATE_NW() { return 4; } // "2026/07/29"
static get N6LDATE_FMT_ISO() { return 5; } // "2026-07-29(Wed) T 04:55:04.123 Z"
static get N6LDATE_FMT_ISO_W() { return 6; } // "2026-07-29 T 04:55:04.123 Z (Wed)"
static get N6LDATE_FMT_ISO_NW() { return 7; } // "2026-07-29 T 04:55:04.123 Z"
static get N6LDATE_FMT_ISO_D() { return 8; } // "2026-07-29(Wed)"
static get N6LDATE_FMT_ISO_D_NW() { return 9; } // "2026-07-29"
// Formatted output (e.g., "2026/07/29 04:55:04 Wed")
toString(fmtsw = 0)
// Custom format output
// e.g., date.format("YYYY-MM-DD hh:mm:ss.SSS (W)")
format(pattern)
// Date object output
toDate()
// Temporal object output
toTemporal()
// Create with current date and time
Now()
// Julian Day
toJD()
fromJD(rh)
// Modified Julian Day
toMJD()
fromMJD(rh)
// Unix time (seconds)
toUNIX()
fromUNIX(rh)
// Unix time (milliseconds)
toUNIXms()
fromUNIXms(rh)
// Serialization
//const dt = new N6LDate(2030, 9, 15, 12, 34, 56, 789).normalize();
//dt.region = "JP";
//const json = JSON.stringify(dt); // toJSON is called automatically
// → {"year":2030,"month":9,"day":15,...}
//const restored = N6LDate.fromJSON(JSON.parse(json));
//console.log(restored.toString()); // 2030/09/15(Sun) 12:34:56.789
toJSON()
static fromJSON(json)
startOf(unit)
endOf(unit)
// Various addition methods // Simply calls adjustCalendar with the argument treated as elapsed days (including fractions)
addYears(rh)
addMonths(rh)
addDays(rh)
addHours(rh)
addMinutes(rh)
addSeconds(rh)
addMSs(rh)
}
Back to Table of contents
Back to NAS6LIB Repository [Links outside the wiki]