From 89105840cc83fe68be511d062290bdccb8958159 Mon Sep 17 00:00:00 2001 From: Kato Twofold Date: Sat, 7 May 2022 14:22:36 +0300 Subject: [PATCH] + Hour Added * Fixed Types --- src/Types/formatTypes.ts | 16 ++++++++-------- src/index.ts | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/src/Types/formatTypes.ts b/src/Types/formatTypes.ts index fecfe0e..d4d89cd 100644 --- a/src/Types/formatTypes.ts +++ b/src/Types/formatTypes.ts @@ -11,7 +11,7 @@ const mertypes = [ "a", "A" ]; /** * Possible formats for the date */ -export type meridiemType = (typeof mertypes)[number]; +export type meridiemType = "a" | "A"; export const isMeridiemType = (x: any): x is meridiemType => mertypes.includes(x); // =-=-=-=-=-=-=-=-=-=-= [ Hours ] =-=-=-=-=-=-=-=-=-=-= @@ -19,19 +19,19 @@ const hourtypes = [ "g", "G", "h", "H" ]; /** * Possible formats for the date */ -export type hourType = (typeof hourtypes)[number]; +export type hourType = "g" | "G" | "h" | "H"; export const isHourType = (x: any): x is hourType => hourtypes.includes(x); // =-=-=-=-=-=-=-=-=-=-= [ Minutes ] =-=-=-=-=-=-=-=-=-=-= -const minutetypes = [ "g", "G", "h", "H" ]; +const minutetypes = [ "i" ]; /** * Possible formats for the date */ -export type minuteType = (typeof minutetypes)[number]; +export type minuteType = "i"; export const isMinuteType = (x: any): x is minuteType => minutetypes.includes(x); // =-=-=-=-=-=-=-=-=-=-= [ Seconds ] =-=-=-=-=-=-=-=-=-=-= -const secondtypes = [ "g", "G", "h", "H" ]; +const secondtypes = [ "s" ]; /** * Possible formats for the date */ @@ -47,7 +47,7 @@ const dtypes = [ "j", "d", "D", "l", "w" ]; /** * Possible formats for the date */ -export type dateType = (typeof dtypes)[number]; +export type dateType = "j" | "d" | "D" | "l" | "w"; export const isDateType = (x: any): x is dateType => dtypes.includes(x); // =-=-=-=-=-=-=-=-=-=-= [ Month ] =-=-=-=-=-=-=-=-=-=-= @@ -55,7 +55,7 @@ const mtypes = [ "n", "m", "M", "F" ]; /** * Possible formats for the month */ -export type monthType = (typeof mtypes)[number]; +export type monthType = "n" | "m" | "M" | "F"; export const isMonthType = (x: any): x is monthType => mtypes.includes(x); // =-=-=-=-=-=-=-=-=-=-= [ Year ] =-=-=-=-=-=-=-=-=-=-= @@ -63,5 +63,5 @@ const yeartypes = [ "L", "o", "y", "Y" ]; /** * Possible formats for the year */ -export type yearType = (typeof yeartypes)[number]; +export type yearType = "L" | "o" | "y" | "Y"; export const isYearType = (x: any): x is yearType => yeartypes.includes(x); \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 4b8d11b..011e617 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import { dayDictionary } from "./Dictionaries/dateDictionary"; import { monthDictionary } from "./Dictionaries/monthDictionary"; -import { commonDateFormats, dateType, isDateType, isMonthType, isYearType, meridiemType, monthType, yearType } from "./Types/formatTypes"; +import { commonDateFormats, dateType, hourType, isDateType, isHourType, isMeridiemType, isMonthType, isYearType, meridiemType, monthType, yearType } from "./Types/formatTypes"; /** @@ -60,6 +60,10 @@ export default class TymeJS { formattedDate += this.getMonth(char); else if ( isYearType(char) ) formattedDate += this.getYear(char); + else if ( isMeridiemType(char) ) + formattedDate += this.getMeridiem(char); + else if ( isHourType(char) ) + formattedDate += this.getHour(char); else { // Just append the char to the result formattedDate += char; @@ -75,11 +79,39 @@ export default class TymeJS { // ############################## [ Time Formats ] ############################## public getMeridiem(t: meridiemType): string { - + // Get the current meridiem + let meridiem = ((this.ts.getHours() + 11) % 12 + 1) > 12 ? "pm" : "am"; + // Check which format to return + if ( t === "A" ) { + return meridiem.toUpperCase(); + } else if ( t === "a" ) { + return meridiem; + } throw new Error(`Invalid format for meridiem ${t}`); } + public getHour(t: hourType): string | number { + // Get current hour + let h = this.ts.getHours(); + + if ( t == "G" ) { + return h; + } else if ( t === "g" ) { + if ( h > 12 ) + return h - 12; + return h; + } else if ( t === "H" ) { + return String(h).padStart(2, "0"); + } else if ( t === "h" ) { + if ( h > 12 ) + return String(h - 12).padStart(2, "0"); + return String(h).padStart(2, "0"); + } + + throw new Error(`Invalid format for Hour ${t}`); + } + // ############################## [ Date Formats ] ##############################