diff --git a/packages/shell-api/src/shell-bson.spec.ts b/packages/shell-api/src/shell-bson.spec.ts index 3d112da28..de656c6a7 100644 --- a/packages/shell-api/src/shell-bson.spec.ts +++ b/packages/shell-api/src/shell-bson.spec.ts @@ -381,6 +381,15 @@ describe('Shell BSON', function () { } expect.fail('expected error'); }); + it('passes through non-string args to `new Date()`', function () { + expect(shellBson.ISODate()).to.be.an.instanceOf(Date); + expect(shellBson.ISODate(0).getTime()).to.equal(0); + expect(shellBson.ISODate(null).getTime()).to.equal(0); + expect(shellBson.ISODate(1234).getTime()).to.equal(1234); + expect(shellBson.ISODate(shellBson.ISODate(1234)).getTime()).to.equal( + 1234 + ); + }); }); describe('BinData', function () { it('expects strings as base 64', function () { diff --git a/packages/shell-api/src/shell-bson.ts b/packages/shell-api/src/shell-bson.ts index bdfd7b6cc..8ae1b375d 100644 --- a/packages/shell-api/src/shell-bson.ts +++ b/packages/shell-api/src/shell-bson.ts @@ -261,8 +261,11 @@ export default function constructShellBson( }, { prototype: bson.Long.prototype } ), - ISODate: function ISODate(input?: string): Date { - if (!input) input = new Date().toISOString(); + ISODate: function ISODate( + input?: string | number | Date | undefined + ): Date { + if (input === undefined) return new Date(); + if (typeof input !== 'string') return new Date(input); const isoDateRegex = /^(?\d{4})-?(?\d{2})-?(?\d{2})([T ](?\d{2})(:?(?\d{2})(:?((?\d{2})(\.(?\d+))?))?)?(?Z|([+-])(\d{2}):?(\d{2})?)?)?$/; const match = isoDateRegex.exec(input);