ect-date-formatter

An Ivy compiled package for formatting dates

$0

8 downloads
exoduscloudtech

ECT Date Formatter

This library was generated with Angular CLI version 18.0.0.

Description

This is a date formatter service which is ESM2022, FESM2022, ESM2020, FESM2020 and FESM2015 compatible. Truth be told this library was put together after using other non-Ivy compiled packages previously, and found those packages to be a struggle to deal with in major version upgrades of the Angular framework. In the end, this library was created after those frustrations.

Usage

Add the EctDateFormatter service to your Modules' or AppComponent's Providers list, then inject the service into the constructor of your class.

For example:

import { Component } from '@angular/core';

import { EctDateFormatterService } from 'ect-date-formatter';

@Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: [ './app.component.css' ], providers: [ EctDateFormatter ] }) export class AppComponent {

public isoDate = ''; public isoDateAndTime = ''; public isoTime = ''; public formattedDate = ''; public formattedTime = '';

constructor(private dateFormatter: EctDateFormatter) { const sampleDate = new Date(2023, 2, 28, 9, 15, 0); this.isoDate = this.dateFormatter.getISO8601DateFormat(sampleDate); this.isoDateAndTime = this.dateFormatter.getISO8601DateAndTimeFormat(sampleDate); this.isoTime = this.dateFormatter.getISO8601TimeFormat(sampleDate); this.formattedDate = this.dateFormatter.formatDate(sampleDate, 'dddd dd MMMM yyyy'); this.formattedTime = this.dateFormatter.formatTime(sampleDate, 'HH:MI:SS'); }

}

In the above example, the property values set are as follows:

  • isoDate = '2023-03-28'
  • isoDateAndTime = '2023-03-28T09:15:00'
  • isoTime = '09:15:00'
  • formattedDate = 'Tuesday 28 March 2023'
  • formattedTime = '09:15:00'

After installing the packages, add the EctDateFormatter service to your Modules' or AppComponent's Providers list, then inject the service into the constructor of your class or component.

Functions

IEctDateFormatterService exposes the following functions:

  • getISO8601DateFormat(date: Date): string
  • getISO8601TimeFormat(date: Date): string
  • getISO8601DateAndTimeFormat(date: Date): string
  • formatDate(date: Date, format: string): string
  • formatTime(date: Date, format: string): string

Date format characters

In the formatDate function, the format argument accepts the following characters with their definition:

  • yy/YY: Two-digit year, e.g. 21
  • yyyy/YYYY: Four-digit year, e.g. 2021
  • m/M: One-digit month for months below 10, e.g. 3
  • mm/MM: Two-digit month, e.g. 03
  • mmm/MMM: Three-letter abbreviation for month, e.g. Mar
  • mmmm/MMMM: Month spelled out in full, e.g. March
  • d/D: One-digit day of the month for days below 10, e.g. 2
  • dd/DD: Two-digit day of the month, e.g. 02
  • ddd/DDD: Three-letter abbreviation for day of the week, e.g. Fri
  • dddd/DDDD: Day of the week spelled out in full, e.g. Friday

Time format characters

In the formatTime function, the format argument accepts the following characters with their definition:

  • h: Hour to 1 digit on 12 hour clock - without leading 0
  • hh: Hour to 2 digits on 12 hour clock - with leading 0
  • H: Hour to 1 digit on 24 hour clock - without leading 0
  • HH: Hour to 2 digits on 24 hour clock - with leading 0
  • m: Minute to 1 digit - without leading 0
  • mm/mi/MI: Minute to 2 digits - with leading 0
  • s/S: Second to 1 digit - without leading 0
  • ss/SS: Second to 2 digits - with leading 0
  • f/F: Tenth of a second e.g. 2009-06-15T13:45:30.6170000 -> 6
  • ff/FF: Hundredths of a second e.g. 2009-06-15T13:45:30.6170000 -> 61
  • fff/FFF: Milliseconds e.g. 6/15/2009 13:45:30.617 -> 617
  • ffff/FFFF: Ten thousandths of a second e.g. 2009-06-15T13:45:30.6175000 -> 6175
  • fffff/FFFFF: Hundred thousandths of a second e.g. 2009-06-15T13:45:30.6175400 -> 61754
  • ffffff/FFFFFF: Millionths of a second e.g. 2009-06-15T13:45:30.6175420 -> 617542
  • t/T: First character of AM/PM
  • tt/TT: Both characters of AM/PM

Dependencies

None

Cost

If you find some benefit from using this package, please consider the time it took to put this together, and why not buy me a coffee? Goodness only knows that most of us would not function without coffee. All donations very much welcomed: (https://www.buymeacoffee.com/exoduscloudtech)

Further help

To get more help on the this ECT Date Formatter, please visit (https://angular-grid.net/free/ect-date-formatter)

Monetize your
open-source work

Supercharge your OSS projects by selling npm packages. Get started in just 5 minutes.