From 4790ced1a40eb66d5d3a4d5d0053787740ef1c4d Mon Sep 17 00:00:00 2001 From: Diego Fernandez Date: Wed, 31 Jul 2019 17:50:55 +0100 Subject: [PATCH 1/2] feat: add format|formats to from-utc pipe and parse-pipe --- src/from-utc.pipe.spec.ts | 31 +++++++++++++++++++++++++++++++ src/from-utc.pipe.ts | 4 ++-- src/parse.pipe.spec.ts | 12 ++++++++++++ src/parse.pipe.ts | 4 ++-- 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/from-utc.pipe.spec.ts b/src/from-utc.pipe.spec.ts index cae5ecf..2b69e2d 100644 --- a/src/from-utc.pipe.spec.ts +++ b/src/from-utc.pipe.spec.ts @@ -62,6 +62,37 @@ describe('UtcPipe', () => { expect(amDateFormat.transform(utcOutput, momentFormatString)).toEqual('2016-12-31'); }); + it('should parse as UTC with a provided format', () => { + const datetimeString = '31/12/2016, 23:02:00'; + const momentFormatString = 'DD/MM/YYYY, HH:mm:ss'; + const utcOutput = utcDatePipe.transform(datetimeString, momentFormatString); + expect(utcOutput).toEqual(expect.any(moment)); + expect(utcOutput.isValid()).toBe(true); + + expect(utcOutput.year()).toBe(2016); + expect(utcOutput.month()).toBe(11); + expect(utcOutput.date()).toBe(31); + }); + + it('should parse as UTC with an array of provided formats', () => { + const datetimeString = '31st 12/2016'; + const momentFormatStrings = ['DD/MM/YYYY, HH:mm:ss', 'Do MM/YYYY']; + const utcOutput = utcDatePipe.transform(datetimeString, momentFormatStrings); + expect(utcOutput).toEqual(expect.any(moment)); + expect(utcOutput.isValid()).toBe(true); + + expect(utcOutput.year()).toBe(2016); + expect(utcOutput.month()).toBe(11); + expect(utcOutput.date()).toBe(31); + }); + + it('should output an invalid moment object for a different formatted input', () => { + const datetimeString = '31/12/2016, 23:02:00'; + const utcDate = utcDatePipe.transform(datetimeString); + expect(utcDate).toEqual(expect.any(moment)); + expect(utcDate.isValid()).toBe(false); + }); + }); }); diff --git a/src/from-utc.pipe.ts b/src/from-utc.pipe.ts index 5dde5a1..b00cfbb 100644 --- a/src/from-utc.pipe.ts +++ b/src/from-utc.pipe.ts @@ -5,7 +5,7 @@ import * as moment from 'moment'; @Pipe({ name: 'amFromUtc' }) export class FromUtcPipe implements PipeTransform { - transform(value: any, ...args: string[]): any { - return moment.utc(value); + transform(value: any, formats?: string|string[], ...args: string[]): any { + return formats ? moment.utc(value, formats) : moment.utc(value); } } diff --git a/src/parse.pipe.spec.ts b/src/parse.pipe.spec.ts index d0743d5..43d98a5 100644 --- a/src/parse.pipe.spec.ts +++ b/src/parse.pipe.spec.ts @@ -33,6 +33,18 @@ describe('ParsePipe', () => { expect(amDateFormat.transform(parseOutput, momentFormatString)).toEqual('2016-02-01'); }); + it('should output a moment object for a string date with array of formats', () => { + const dateString = '15--09//13'; + const formatInputStrings = ['YYYY#MM#DD', 'YY--MM//DD']; + const parsedMoment = parsePipe.transform(dateString, formatInputStrings); + expect(parsedMoment).toEqual(expect.any(moment)); + expect(parsedMoment.isValid()).toBe(true); + + expect(parsedMoment.year()).toBe(2015); + expect(parsedMoment.month()).toBe(8); + expect(parsedMoment.date()).toBe(13); + }); + }); }); diff --git a/src/parse.pipe.ts b/src/parse.pipe.ts index 0b1ad25..edd5b58 100644 --- a/src/parse.pipe.ts +++ b/src/parse.pipe.ts @@ -5,7 +5,7 @@ const momentConstructor = moment; @Pipe({ name: 'amParse' }) export class ParsePipe implements PipeTransform { - transform(value: string, format: string): moment.Moment { - return momentConstructor(value, format); + transform(value: string, formats: string|string[]): moment.Moment { + return momentConstructor(value, formats); } } From e1f79feff98791579f66c85c7e07d382268faafa Mon Sep 17 00:00:00 2001 From: Diego Fernandez Date: Wed, 14 Aug 2019 16:46:22 +0100 Subject: [PATCH 2/2] docs: update the README --- README.md | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/README.md b/README.md index 7da0d04..ea3a401 100644 --- a/README.md +++ b/README.md @@ -178,6 +178,26 @@ Parses a custom-formatted date into a moment object that can be used with the ot Prints `Last updated: January 24, 2016` +The pipe can also accept an array of formats as parameter. + +``` typescript +@Component({ + selector: 'app', + template: ` + Last updated: {{'24/01/2014 22:00' | amParse: formats | amDateFormat:'LL'}} + ` +}) +export class App { + + formats: string[] = ['DD/MM/YYYY HH:mm:ss', 'DD/MM/YYYY HH:mm']; + + constructor() { } + +} +``` + +Prints `Last updated: January 24, 2016` + ## amLocal pipe Converts UTC time to local time. @@ -291,6 +311,37 @@ Parses the date as UTC and enables mode for subsequent moment operations (such a Prints `Last updated: 2017-01-01` +It's also possible to specify a different format than the standard ISO8601. + +``` typescript +@Component({ + selector: 'app', + template: ` + Last updated: {{ '31/12/2016 23:00-01:00' | amFromUtc: 'DD/MM/YYYY HH:mmZZ' | amDateFormat: 'YYYY-MM-DD' }} + ` +}) +``` + +Or even an array of formats: + +``` typescript +@Component({ + selector: 'app', + template: ` + Last updated: {{ '31/12/2016 23:00-01:00' | amFromUtc: formats | amDateFormat: 'YYYY-MM-DD' }} + ` +}) +export class App { + + formats: string[] = ['DD/MM/YYYY HH:mm:ss', 'DD/MM/YYYY HH:mmZZ']; + + constructor() { } + +} +``` + +Both examples above will print `Last updated: 2017-01-01` + ## amUtc pipe Enables UTC mode for subsequent moment operations (such as displaying the time in UTC).