Skip to content

Commit 886c801

Browse files
committed
refrenceDate support for fromObject
1 parent cafc4ee commit 886c801

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/datetime.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,8 +745,10 @@ export default class DateTime {
745745
* @param {string} [opts.locale='system\'s locale'] - a locale to set on the resulting DateTime instance
746746
* @param {string} opts.outputCalendar - the output calendar to set on the resulting DateTime instance
747747
* @param {string} opts.numberingSystem - the numbering system to set on the resulting DateTime instance
748+
* @param {DateTime|Date|Object} opts.refrenceDate - the reference date to taken for missing parts
748749
* @example DateTime.fromObject({ year: 1982, month: 5, day: 25}).toISODate() //=> '1982-05-25'
749750
* @example DateTime.fromObject({ year: 1982 }).toISODate() //=> '1982-01-01'
751+
* @example DateTime.fromObject({ year: 1982 }, { refrenceDate: { day: 10 } }).toISODate() //=> '1982-01-10'
750752
* @example DateTime.fromObject({ hour: 10, minute: 26, second: 6 }) //~> today at 10:26:06
751753
* @example DateTime.fromObject({ hour: 10, minute: 26, second: 6 }, { zone: 'utc' }),
752754
* @example DateTime.fromObject({ hour: 10, minute: 26, second: 6 }, { zone: 'local' })
@@ -766,7 +768,9 @@ export default class DateTime {
766768
const normalized = normalizeObject(obj, normalizeUnitWithLocalWeeks);
767769
const { minDaysInFirstWeek, startOfWeek } = usesLocalWeekValues(normalized, loc);
768770

769-
const tsNow = Settings.now(),
771+
const tsNow = isUndefined(opts.refrenceDate)
772+
? friendlyDateTime(opts.refrenceDate).toUnixInteger()
773+
: opts.refrenceDate,
770774
offsetProvis = !isUndefined(opts.specificOffset)
771775
? opts.specificOffset
772776
: zoneToUse.offset(tsNow),

test/datetime/create.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,12 @@ test("DateTime.fromObject takes a undefined to mean {}", () => {
883883
expect(res.year).toBe(new Date().getFullYear());
884884
});
885885

886+
test("DateTime.fromObject respects `refrenceDate`", () => {
887+
const res = DateTime.fromObject(undefined, { refrenceDate: { day: 10 } });
888+
expect(res.year).toBe(new Date().getFullYear());
889+
expect(res.day).toBe(10);
890+
});
891+
886892
test("private language subtags don't break unicode subtags", () => {
887893
const res = DateTime.fromObject(
888894
{},

0 commit comments

Comments
 (0)