Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions PLUGINS.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,45 @@ In the first example above, if you want `parseTZ()` to parse the time as PST, yo
date.parse('Nov 7 2021 1:59:59 -0800', 'MMM D YYYY H:mm:ss Z'); // => 2021-11-07T09:59:59Z
```

#### Token Extension

This plugin also adds tokens for time zone name to the formatter.

**formatter:**

| token | meaning | output examples |
|:------|:----------------------------|:----------------------|
| z | time zone name abbreviation | PST, EST |
| zz | time zone name | Pacific Standard Time |

The `z` and `zz` are lowercase. Also, currently it does not support output other than English.

**parser:**

There is no change.

```javascript
const date = require('date-and-time');
// Import "timezone" plugin.
const timezone = require('date-and-time/plugin/timezone');

// Apply "timezone" plugin to the library.
date.plugin(timezone);

const d1 = new Date(Date.UTC(2021, 2, 14, 9, 59, 59, 999));
date.format(d1, 'MMMM DD YYYY H:mm:ss.SSS zz');
// March 14 2021 1:59:59.999 Pacific Standard Time

date.format(d1, 'MMMM DD YYYY H:mm:ss.SSS zz', true);
// March 14 2021 9:59:59.999 Coordinated Universal Time

date.formatTZ(d1, 'MMMM DD YYYY H:mm:ss.SSS z', 'Asia/Tokyo');
// March 14 2021 18:59:59.999 JST

// Transforms the date string from EST (Eastern Standard Time) to PDT (Pacific Daylight Time).
date.transform('2021-11-07T03:59:59 UTC-0500', 'YYYY-MM-DD[T]HH:mm:ss [UTC]Z', 'MMMM D YYYY H:mm:ss z');
// November 7 2021 1:59:59 PDT
```
---

### two-digit-year
Expand Down
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ npm i date-and-time

## Recent Changes

- 3.4.0
- Added `zz` (time zone name) and `z` (time zone name abbreviation) tokens to the `timezone` plugin.
- Fixed an issue where token extensions by other plugins were not reflected in functions provided by the `timezone` plugin.

- 3.3.0
- Refactored `format()`, `isValid()`, and `preparse()`, further improved performance.

- 3.2.0
- Refactored `compile()`, `format()`, and `preparse()`, slightly improved performance.

- 3.1.1
- Fixed an issue where `format()` could output incorrect UTC times in locales with daylight savings time.
- Refactored `formatTZ()` of `timezone` plugin.

## Usage

- ES Modules:
Expand Down Expand Up @@ -179,12 +179,14 @@ Available tokens and their meanings are as follows:

You can also use the following tokens by importing plugins. See [PLUGINS.md](./PLUGINS.md) for details.

| token | meaning | examples of output |
|:------|:-------------------------------------|:-------------------|
| DDD | ordinal notation of date | 1st, 2nd, 3rd |
| AA | meridiem (uppercase with ellipsis) | A.M., P.M. |
| a | meridiem (lowercase) | am, pm |
| aa | meridiem (lowercase with ellipsis) | a.m., p.m. |
| token | meaning | examples of output |
|:------|:-------------------------------------|:----------------------|
| DDD | ordinal notation of date | 1st, 2nd, 3rd |
| AA | meridiem (uppercase with ellipsis) | A.M., P.M. |
| a | meridiem (lowercase) | am, pm |
| aa | meridiem (lowercase with ellipsis) | a.m., p.m. |
| z | time zone name abbreviation | PST, EST |
| zz | time zone name | Pacific Standard Time |

#### Note 1. Comments

Expand Down
2 changes: 1 addition & 1 deletion date-and-time.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ export type Extension = {
export function extend(extension: Extension): void;

/** Plugin installer */
export type Plugin = (proto: unknown, localized_proto?: unknown) => string;
export type Plugin = (proto: unknown, date?: unknown) => string;

/**
* Importing plugins
Expand Down
9 changes: 4 additions & 5 deletions date-and-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@
_formatter: _formatter,
_parser: _parser
},
localized_proto,
date;

/**
Expand Down Expand Up @@ -177,6 +176,7 @@
u.getMilliseconds = u.getUTCMilliseconds;
u.getDay = u.getUTCDay;
u.getTimezoneOffset = function () { return 0; };
u.getTimezoneName = function () { return 'UTC'; };
return u;
}
return dateObj;
Expand Down Expand Up @@ -453,7 +453,6 @@
}
};

localized_proto = extend(proto);
date = extend(proto);

/**
Expand All @@ -474,8 +473,8 @@
var formatter = extend(_formatter, extension.formatter, true, res);
var parser = extend(_parser, extension.parser, true, res);

date._formatter = localized_proto._formatter = formatter;
date._parser = localized_proto._parser = parser;
date._formatter = formatter;
date._parser = parser;

for (var plugin in plugins) {
date.extend(plugins[plugin]);
Expand Down Expand Up @@ -512,7 +511,7 @@
var install = typeof plugin === 'function' ? plugin : date.plugin[plugin];

if (install) {
date.extend(plugins[install(proto, localized_proto)] || {});
date.extend(plugins[install(proto, date)] || {});
}
};

Expand Down
2 changes: 1 addition & 1 deletion date-and-time.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading