Skip to content

A small date formatting utility module for just my needs

License

Notifications You must be signed in to change notification settings

GaryB432/gb-date-formatter

Repository files navigation

gb-date-formatter

Build Status

A partial re-engineering of AngularJs's date formatting.

Install

$ npm install gb-date-formatter

API

To use the DateFormatter class in a TypeScript file

import { DateFormatter } from "gb-date-formatter";

const formatter = new DateFormatter();
console.log(formatter.format(new Date("1924-02-25T07:44:40.755Z"), "MMMM y"));
// February 1924

To use the DateFormatter class in a JavaScript file

var DateFormatter = require("gb-date-formatter").DateFormatter;

var formatter = new DateFormatter();
console.log(formatter.format(new Date("1924-02-25T07:44:40.755Z"), "MMMM y"));
// February 1924

To use the DateFormatter class in an Angular pipe

wrap the gb-date-formatter in an Angular Pipe

import { Pipe, PipeTransform } from '@angular/core';
import { DateFormatter, FormatString } from 'gb-date-formatter';

@Pipe({ name: 'gbCustomDate' })
export class CustomDatePipe implements PipeTransform {
  private static namedFormats: { [name: string]: FormatString; } = {
    medium: 'MMMM y',
    long: 'EEE MMM d, y h:mm a'
  };
  private formatter: DateFormatter = new DateFormatter('en-US');

  public transform(value: Date, fmt: string): string {
    return this.formatter.format(value, CustomDatePipe.namedFormats[fmt]);

  }
}

and add the directive to a template

<td [title]="friend.birthday | gbCustomDate:'long'">{{friend.birthday | gbCustomDate:'medium'}}</td>

About

A small date formatting utility module for just my needs

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published