@bbn/bbn 2.0.130 → 2.0.132

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -65,6 +65,8 @@ export declare abstract class bbnDt<TValue extends bbnDtTemporal> {
65
65
  date(v?: any): string | bbnDt<any>;
66
66
  datetime(v?: any): string | bbnDt<any>;
67
67
  time(v?: any): string | bbnDt<any>;
68
+ fdate(long?: boolean): string;
69
+ ftime(withSeconds?: boolean): string;
68
70
  week(): number;
69
71
  get YYYY(): string;
70
72
  get YY(): string;
@@ -89,6 +91,7 @@ export declare abstract class bbnDt<TValue extends bbnDtTemporal> {
89
91
  get W(): string;
90
92
  unix(ms?: boolean | number): number | bbnDt<any>;
91
93
  format(format?: string): string;
94
+ calendar(short?: boolean): string;
92
95
  matchFormat(value: any, format: string): boolean;
93
96
  getWeekday(n: 0 | 1 | 2 | 3 | 4 | 5 | 6, mode?: string, locale?: string): string | object;
94
97
  getWeekdayIndex: (name: string, locale?: string) => number;
@@ -464,6 +464,18 @@ export class bbnDt {
464
464
  }
465
465
  return this.HH + ':' + this.II + ':' + this.SS;
466
466
  }
467
+ fdate(long = false) {
468
+ if (!this.value) {
469
+ return '';
470
+ }
471
+ return bbn.dt.locales.formatters.short.format(new Date(this.toEpochMs()));
472
+ }
473
+ ftime(withSeconds = true) {
474
+ if (!this.value) {
475
+ return '';
476
+ }
477
+ return this.format('HH:II' + (withSeconds ? ':SS' : ''));
478
+ }
467
479
  week() {
468
480
  if (!this.value) {
469
481
  return undefined;
@@ -669,6 +681,22 @@ export class bbnDt {
669
681
  }
670
682
  return str;
671
683
  }
684
+ calendar(short) {
685
+ const now = this.constructor.now();
686
+ const startThis = this.startOf("day");
687
+ const startNow = now.startOf("day");
688
+ const diffDays = startThis.diff(startNow, "day");
689
+ const rtf = new Intl.RelativeTimeFormat(bbn.env.lang, { numeric: "auto" });
690
+ let phrase;
691
+ if (diffDays >= -6 && diffDays <= 6) {
692
+ phrase = rtf.format(diffDays, "day");
693
+ }
694
+ else {
695
+ const diffWeeks = Math.floor(diffDays / 7);
696
+ phrase = rtf.format(diffWeeks, "week");
697
+ }
698
+ return `${phrase} ${this.ftime()}`;
699
+ }
672
700
  matchFormat(value, format) {
673
701
  try {
674
702
  this.parse(value, format);
@@ -233,6 +233,9 @@ const buildLocaleFromIntl = () => {
233
233
  const fmtMonthShort = new Intl.DateTimeFormat(langs, { month: 'short' });
234
234
  const fmtWeekLong = new Intl.DateTimeFormat(langs, { weekday: 'long' });
235
235
  const fmtWeekShort = new Intl.DateTimeFormat(langs, { weekday: 'short' });
236
+ const fmtShort = new Intl.DateTimeFormat(langs, { dateStyle: 'short', timeStyle: 'short' });
237
+ const fmtLong = new Intl.DateTimeFormat(langs, { dateStyle: 'long', timeStyle: 'long' });
238
+ const rtf = new Intl.RelativeTimeFormat(langs, { numeric: 'auto' });
236
239
  // Create 12 dates for months (2020 chosen arbitrarily)
237
240
  const monthsLong = [];
238
241
  const monthsShort = [];
@@ -253,6 +256,11 @@ const buildLocaleFromIntl = () => {
253
256
  }
254
257
  const { date, time, datetime } = getCommonFormatsForLocale(langs);
255
258
  extend(bbn.dt.locales, {
259
+ formatters: {
260
+ short: fmtShort,
261
+ long: fmtLong,
262
+ relative: rtf
263
+ },
256
264
  monthsLong,
257
265
  monthsShort,
258
266
  weekdaysLong,
@@ -26,6 +26,6 @@ export default function calendar(d, wrong_result = false) {
26
26
  if (!r.isValid) {
27
27
  return wrong_result && isString(wrong_result) ? wrong_result : '';
28
28
  }
29
- return r.fdate();
29
+ return r.calendar();
30
30
  }
31
31
  ;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "2.0.130",
3
+ "version": "2.0.132",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",