nestjs-date-format-interceptor
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

nestjs-date-format-interceptor

Easy formatting for output dates in NestJS

This NPM provides an easy way to give a format to api response dates in NestJs

How to install.

npm install nestjs-date-format-interceptor

Binding in controller

import { DateFormatInterceptor, DateFormat } from "utilities/lib/date";
 
// ...
 
@UseInterceptors(new DateFormatInterceptor({ maxDeep: 6, format: DateFormat.toISOString }))
export class CatsController {}

Binding globally

import { DateFormatInterceptor, DateFormat } from "utilities/lib/date";
 
// ...
 
const app = await NestFactory.create(AppModule);
 
app.useGlobalInterceptors(new DateFormatInterceptor({ maxDeep: 6, format: DateFormat.toISOString }));

Parameter options

 
{
  maxDeepnumber = 6;
  formatDateFormat = DateFormat.toISOString;
  fnFormat?: (value: Date) => string;
}
 
  • maxDeep: Max deep of nested objects to check dates
  • format: The format function to build the date-string
  • fnFormat?: Optional, Custom format function.
format options
Format Result
toDateString "Tue Aug 19 1975"
toISOString "1975-08-20T05:15:30.000Z"
toJSON "1975-08-20T05:15:30.000Z"
toString "Tue Aug 19 1975 23:15:30 GMT-0600 (Central Standard Time)"
toTimeString "23:15:30 GMT-0600 (Central Standard Time)"
toUTCString "Wed, 20 Aug 1975 05:15:30 GMT"
fnFormat Custom format example
 
import { DateFormatInterceptor, DateFormat } from 'utilities/lib/date';
 
...
 
const app = await NestFactory.create(AppModule);
 
app.useGlobalInterceptors(
  new DateFormatInterceptor({
    maxDeep: 6,
    format: DateFormat.toISOString,
    fnFormat: date => `Date in Costa Rica is ${date.toLocaleString('es-CR')}`,
  }),
);
 
Result:
Date in Costa Rica is 7/14/2020, 8:01:15 PM

Author

Contributors

Readme

Keywords

Package Sidebar

Install

npm i nestjs-date-format-interceptor

Weekly Downloads

70

Version

1.0.0

License

ISC

Unpacked Size

12.9 kB

Total Files

17

Last publish

Collaborators

  • ariassd