diff --git a/src/Types/formatTypes.ts b/src/Types/formatTypes.ts index 3739297..5f79410 100644 --- a/src/Types/formatTypes.ts +++ b/src/Types/formatTypes.ts @@ -1,2 +1,35 @@ -export type monthType = "single" | "double" | "short" | "long"; -export type dateType = "single" | "double" | "short" | "long" | "dowsingle" | "dowdouble"; \ No newline at end of file +// All mappings according to: https://www.php.net/manual/en/datetime.format.php#refsect1-datetime.format-parameters + +// ############################## [ Common Formats ] ############################## + +export type commonDateFormats = "d/m/Y" | "m/d/Y" | "d/m/Y h:i A" | "d/m/Y H:i" | "m/d/Y h:i A" | "m/d/Y H:i"; + +// ############################## [ Time Formats ] ############################## + + + +// ############################## [ Date Formats ] ############################## + +// =-=-=-=-=-=-=-=-=-=-= [ Date ] =-=-=-=-=-=-=-=-=-=-= +const dtypes = [ "j", "d", "D", "l", "w" ]; +/** + * Possible formats for the date + */ +export type dateType = (typeof dtypes)[number]; +export const isDateType = (x: any): x is dateType => dtypes.includes(x); + +// =-=-=-=-=-=-=-=-=-=-= [ Month ] =-=-=-=-=-=-=-=-=-=-= +const mtypes = [ "n", "m", "M", "F" ]; +/** + * Possible formats for the month + */ +export type monthType = (typeof mtypes)[number]; +export const isMonthType = (x: any): x is monthType => mtypes.includes(x); + +// =-=-=-=-=-=-=-=-=-=-= [ Year ] =-=-=-=-=-=-=-=-=-=-= +const yeartypes = [ "L", "o", "y", "Y" ]; +/** + * Possible formats for the year + */ +export type yearType = (typeof yeartypes)[number]; +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 e73300f..cb5a27b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,6 @@ -import { dayDictionary } from "./Dictionaries/dateDictionary"; -import { monthDictionary } from "./Dictionaries/monthDictionary"; -import { dateType, monthType } from "./Types/formatTypes"; - - - +import { dayDictionary } from "./Dictionaries/dateDictionary"; +import { monthDictionary } from "./Dictionaries/monthDictionary"; +import { commonDateFormats, dateType, monthType, yearType } from "./Types/formatTypes"; /** @@ -35,59 +32,110 @@ export default class TymeJS { return this.ts; } + /** + * @returns a string representation of a date. The format of the string depends on the locale. + */ + public toString(): string { + // Return the current date as a string + return this.getDateObject().toString(); + } + /** * Format the current timestamp to a workable format * @param dateTimeFormat * @returns The date formatted in the provided dateTimeFormat */ - public format(dateTimeFormat: string): string { + public format(dateTimeFormat: string | commonDateFormats): string { /** * The formatted date */ let formattedDate = ``; + // Go through all of the characters of the dateTimeFormat string + for ( let char of Array.from(dateTimeFormat) ) { + + } + // Return the formatted date return formattedDate; } + + // ############################## [ Time Formats ] ############################## + + + // ############################## [ Date Formats ] ############################## + /** - * Get the current month + * + * @param t The format to get the date in + * @returns A string of the formatted date */ - public getMonth(mType: monthType): string { - // get the index of the current month - let monthIndex: number = this.ts.getMonth(); + public getDate(t: dateType): string | number { - if ( mType === "single" ) { - return String(monthIndex + 1); - } else if ( mType === "double" ) { - return String(monthIndex + 1).padStart(2, "0"); - } else if ( mType === "short" ) { - return monthDictionary[monthIndex].slice(0, 3); - } else { - return monthDictionary[monthIndex]; - } - - } - - public getDate(dType: dateType): string { - - if ( dType == "single" ) { + if ( t == "j" ) { // get the index of the current day let currentDate: number = this.ts.getDate(); - return String(currentDate); - } else if ( dType == "double" ) { + return currentDate; + } else if ( t == "d" ) { // get the index of the current day let currentDate: number = this.ts.getDate(); return String(currentDate).padStart(2, "0"); - } else if ( dType == 'dowsingle' ) { + } else if ( t == 'w' ) { return String(this.ts.getDay()); - } else if ( dType == 'dowdouble' ) { - return String(this.ts.getDay()).padStart(2, "0"); - } else if ( dType == "short") { - return String(dayDictionary[this.ts.getDay() - 1]); - } else { - return String(dayDictionary[this.ts.getDay() - 1]).slice(0, 3); + } else if ( t == "l") { + return String(dayDictionary[this.ts.getDay()]); + } else if ( t == 'D' ) { + return String(dayDictionary[this.ts.getDay()]).slice(0, 3); } + + throw new Error(`Invalid format for date ${t}`); + } + + /** + * Format the current date object's month + * @param t The format to get the date in + * @returns A string of the formatted date + */ + public getMonth(t: monthType): string | number { + // get the index of the current month + let monthIndex: number = this.ts.getMonth(); + + if ( t === "n" ) { + return (monthIndex + 1); + } else if ( t === "m" ) { + return String(monthIndex + 1).padStart(2, "0"); + } else if ( t === "M" ) { + return monthDictionary[monthIndex].slice(0, 3); + } else if ( t === "F" ) { + return monthDictionary[monthIndex]; + } + + throw new Error(`Invalid format for date ${t}`); + } + + /** + * Format the current date object's year + * @param t The format to get the date in + * @returns A string of the formatted date + */ + public getYear(t: yearType): string | number { + + if ( t == "L" ) { + return (TymeJS.isLeapYear(this.ts.getFullYear())) ? 1 : 0; + } else if ( t == 'Y' ) { + return this.ts.getFullYear(); + } else if ( t == 'y' ) { + return String(this.ts.getFullYear()).slice(2); + } else if ( t == 'o' ) { + throw new Error(`Not yet implemented`); + } + + throw new Error(`Invalid format for year ${t}`); + } + + public static isLeapYear(year: number): boolean { + return new Date(year, 1, 29).getDate() === 29; } } diff --git a/tests/dateTest.ts b/tests/dateTest.ts index c5c352e..cdac4b6 100644 --- a/tests/dateTest.ts +++ b/tests/dateTest.ts @@ -1,18 +1,18 @@ import TymeJS from "../src"; -let startDate: Date = new Date(); +let d: Date = new Date(); for ( let i = 0; i < 7; i++ ) { - const a = new TymeJS(startDate); - console.log(startDate, { - single: a.getDate("single"), - double: a.getDate("double"), - dowsingle: a.getDate("dowsingle"), - dowdouble: a.getDate("dowdouble"), - full: a.getDate("long"), - short: a.getDate("short"), + + const a = new TymeJS(d); + console.log(d, { + single: a.getDate("j"), + double: a.getDate("d"), + dowdouble: a.getDate("w"), + full: a.getDate("l"), + short: a.getDate("D"), }); - startDate.setDate(startDate.getDate() + 1); + d.setDate(d.getDate() + 1); } diff --git a/tests/monthTest.ts b/tests/monthTest.ts index 23c4ed8..8e63352 100644 --- a/tests/monthTest.ts +++ b/tests/monthTest.ts @@ -8,10 +8,10 @@ for ( let i = 0; i < 12; i++ ) { const a = new TymeJS(currentMonth); console.log({ - single: a.getMonth("single"), - double: a.getMonth("double"), - full: a.getMonth("long"), - short: a.getMonth("short"), + single: a.getMonth("n"), + double: a.getMonth("m"), + full: a.getMonth("F"), + short: a.getMonth("M"), }); }