Skip to content

Commit

Permalink
Document working with JS Date objects
Browse files Browse the repository at this point in the history
  • Loading branch information
bjuppa committed Aug 7, 2023
1 parent b1ec416 commit fefb7d9
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,50 @@ name that is part of a longer string, use
[`parseTimezone`](https://deno.land/x/complaindate/mod.ts?s=parseTimezone)
directly to both sanitize and validate the result.

## Working with JavaScript `Date` objects

JavaScript `Date` objects, that is _instants_, can be created from any date-time
**shaped** objects in a specified timezone:

```ts
const noon2023Feb3InSweden = createInstant("Europe/Stockholm")({
year: 2023,
month: 2,
day: 3,
hour: 12,
minute: 0,
second: 0,
millisecond: 0,
}); // 2023-02-03T11:00:00.000Z
```

These examples combine existing plain-date and plain-time objects:

```ts
const jsDateInSweden = createInstant("Europe/Stockholm")({
...jan1,
...midday,
}); // 2023-01-01T11:00:00.000Z

const jsDateInSystemTz = createLocalInstant({
...jan1,
...midday,
});

const jsDateInUtc = createUtcInstant({
...jan1,
...midday,
}); // 2023-01-01T12:00:00.000Z
```

For UTC, that last example can also be written using the
[`toUtcInstant`](https://deno.land/x/complaindate/mod.ts?s=toUtcInstant) method
of the plain-date object, passing an optional wall-time shaped object:

```ts
jan1.toUtcInstant(...midday); // 2023-01-01T12:00:00.000Z
```

## Why another JavaScript date-time library?

Most other date-time libraries either don't provide any clear strategy for
Expand Down

0 comments on commit fefb7d9

Please sign in to comment.