First version of the working date formatter

This commit is contained in:
Daniel Legt 2022-05-07 14:02:55 +03:00
parent 8d3de497e9
commit d97a80bb39
3 changed files with 25 additions and 5 deletions

View File

@ -1,6 +1,6 @@
import { dayDictionary } from "./Dictionaries/dateDictionary";
import { monthDictionary } from "./Dictionaries/monthDictionary";
import { commonDateFormats, dateType, monthType, yearType } from "./Types/formatTypes";
import { commonDateFormats, dateType, isDateType, isMonthType, isYearType, monthType, yearType } from "./Types/formatTypes";
/**
@ -45,7 +45,7 @@ export default class TymeJS {
* @param dateTimeFormat
* @returns The date formatted in the provided dateTimeFormat
*/
public format(dateTimeFormat: string | commonDateFormats): string {
public format(dateTimeFormat: commonDateFormats | string ): string {
/**
* The formatted date
*/
@ -53,6 +53,17 @@ export default class TymeJS {
// Go through all of the characters of the dateTimeFormat string
for ( let char of Array.from(dateTimeFormat) ) {
// Check which one of the formatters can handle this
if ( isDateType(char) )
formattedDate += this.getDate(char);
else if ( isMonthType(char) )
formattedDate += this.getMonth(char);
else if ( isYearType(char) )
formattedDate += this.getYear(char);
else {
// Just append the char to the result
formattedDate += char;
}
}

9
tests/fullTest.ts Normal file
View File

@ -0,0 +1,9 @@
import TymeJS from "../src";
const a = new TymeJS();
console.log(a.format("d/m/Y"));
console.log(a.format("d/m/Y h:i A"));
console.log(a.format("Y-m-d"));
console.log(a.format("Y-m-d H:i:s"));