* Changed the valid inputs to actually conform to a standard

* Fixed a bunch of bugs
+ Year
This commit is contained in:
Daniel Legt 2022-05-07 13:49:57 +03:00
parent c596812d30
commit a88351922f
4 changed files with 132 additions and 51 deletions

View File

@ -1,2 +1,35 @@
export type monthType = "single" | "double" | "short" | "long"; // All mappings according to: https://www.php.net/manual/en/datetime.format.php#refsect1-datetime.format-parameters
export type dateType = "single" | "double" | "short" | "long" | "dowsingle" | "dowdouble";
// ############################## [ 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);

View File

@ -1,9 +1,6 @@
import { dayDictionary } from "./Dictionaries/dateDictionary"; import { dayDictionary } from "./Dictionaries/dateDictionary";
import { monthDictionary } from "./Dictionaries/monthDictionary"; import { monthDictionary } from "./Dictionaries/monthDictionary";
import { dateType, monthType } from "./Types/formatTypes"; import { commonDateFormats, dateType, monthType, yearType } from "./Types/formatTypes";
/** /**
@ -35,59 +32,110 @@ export default class TymeJS {
return this.ts; 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 * Format the current timestamp to a workable format
* @param dateTimeFormat * @param dateTimeFormat
* @returns The date formatted in the provided dateTimeFormat * @returns The date formatted in the provided dateTimeFormat
*/ */
public format(dateTimeFormat: string): string { public format(dateTimeFormat: string | commonDateFormats): string {
/** /**
* The formatted date * The formatted date
*/ */
let formattedDate = ``; let formattedDate = ``;
// Go through all of the characters of the dateTimeFormat string
for ( let char of Array.from(dateTimeFormat) ) {
}
// Return the formatted date // Return the formatted date
return formattedDate; 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 { public getDate(t: dateType): string | number {
// get the index of the current month
let monthIndex: number = this.ts.getMonth();
if ( mType === "single" ) { if ( t == "j" ) {
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" ) {
// get the index of the current day // get the index of the current day
let currentDate: number = this.ts.getDate(); let currentDate: number = this.ts.getDate();
return String(currentDate); return currentDate;
} else if ( dType == "double" ) { } else if ( t == "d" ) {
// get the index of the current day // get the index of the current day
let currentDate: number = this.ts.getDate(); let currentDate: number = this.ts.getDate();
return String(currentDate).padStart(2, "0"); return String(currentDate).padStart(2, "0");
} else if ( dType == 'dowsingle' ) { } else if ( t == 'w' ) {
return String(this.ts.getDay()); return String(this.ts.getDay());
} else if ( dType == 'dowdouble' ) { } else if ( t == "l") {
return String(this.ts.getDay()).padStart(2, "0"); return String(dayDictionary[this.ts.getDay()]);
} else if ( dType == "short") { } else if ( t == 'D' ) {
return String(dayDictionary[this.ts.getDay() - 1]); return String(dayDictionary[this.ts.getDay()]).slice(0, 3);
} else {
return String(dayDictionary[this.ts.getDay() - 1]).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;
} }
} }

View File

@ -1,18 +1,18 @@
import TymeJS from "../src"; import TymeJS from "../src";
let startDate: Date = new Date(); let d: Date = new Date();
for ( let i = 0; i < 7; i++ ) { for ( let i = 0; i < 7; i++ ) {
const a = new TymeJS(startDate);
console.log(startDate, { const a = new TymeJS(d);
single: a.getDate("single"), console.log(d, {
double: a.getDate("double"), single: a.getDate("j"),
dowsingle: a.getDate("dowsingle"), double: a.getDate("d"),
dowdouble: a.getDate("dowdouble"), dowdouble: a.getDate("w"),
full: a.getDate("long"), full: a.getDate("l"),
short: a.getDate("short"), short: a.getDate("D"),
}); });
startDate.setDate(startDate.getDate() + 1); d.setDate(d.getDate() + 1);
} }

View File

@ -8,10 +8,10 @@ for ( let i = 0; i < 12; i++ ) {
const a = new TymeJS(currentMonth); const a = new TymeJS(currentMonth);
console.log({ console.log({
single: a.getMonth("single"), single: a.getMonth("n"),
double: a.getMonth("double"), double: a.getMonth("m"),
full: a.getMonth("long"), full: a.getMonth("F"),
short: a.getMonth("short"), short: a.getMonth("M"),
}); });
} }