First version of the working date formatter
This commit is contained in:
parent
8d3de497e9
commit
d97a80bb39
15
src/index.ts
15
src/index.ts
|
@ -1,6 +1,6 @@
|
||||||
import { dayDictionary } from "./Dictionaries/dateDictionary";
|
import { dayDictionary } from "./Dictionaries/dateDictionary";
|
||||||
import { monthDictionary } from "./Dictionaries/monthDictionary";
|
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
|
* @param dateTimeFormat
|
||||||
* @returns The date formatted in the provided dateTimeFormat
|
* @returns The date formatted in the provided dateTimeFormat
|
||||||
*/
|
*/
|
||||||
public format(dateTimeFormat: string | commonDateFormats): string {
|
public format(dateTimeFormat: commonDateFormats | string ): string {
|
||||||
/**
|
/**
|
||||||
* The formatted date
|
* The formatted date
|
||||||
*/
|
*/
|
||||||
|
@ -53,6 +53,17 @@ export default class TymeJS {
|
||||||
|
|
||||||
// Go through all of the characters of the dateTimeFormat string
|
// Go through all of the characters of the dateTimeFormat string
|
||||||
for ( let char of Array.from(dateTimeFormat) ) {
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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"));
|
||||||
|
|
Reference in New Issue