First version of the working date formatter
This commit is contained in:
parent
8d3de497e9
commit
d97a80bb39
|
@ -5,7 +5,7 @@
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "esbuild src/index.ts --bundle --minify --sourcemap --target=chrome58,firefox57,safari11,edge16 --outfile=dist/tyme.js",
|
"build": "esbuild src/index.ts --bundle --minify --sourcemap --target=chrome58,firefox57,safari11,edge16 --outfile=dist/tyme.js",
|
||||||
"test": "tsc"
|
"test": "tsc"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"time",
|
"time",
|
||||||
|
@ -18,4 +18,4 @@
|
||||||
"repository": {
|
"repository": {
|
||||||
"url": "https://github.com/JustKato/Tyme.js"
|
"url": "https://github.com/JustKato/Tyme.js"
|
||||||
}
|
}
|
||||||
}
|
}
|
17
src/index.ts
17
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,7 +53,18 @@ 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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the formatted date
|
// Return the formatted date
|
||||||
|
|
|
@ -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