+ Hour Added

* Fixed Types
This commit is contained in:
Daniel Legt 2022-05-07 14:22:36 +03:00
parent 48a6d18c24
commit 89105840cc
2 changed files with 42 additions and 10 deletions

View File

@ -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);

View File

@ -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 ] ##############################