Days implementation

+ getDate
+ dayTypes
This commit is contained in:
Daniel Legt 2022-05-06 01:07:13 +03:00
parent 1112a92023
commit c596812d30
6 changed files with 67 additions and 6 deletions

View File

@ -14,5 +14,8 @@
"format" "format"
], ],
"author": "Kato Twofold", "author": "Kato Twofold",
"license": "MIT" "license": "MIT",
"repository": {
"url": "https://github.com/JustKato/Tyme.js"
}
} }

View File

@ -0,0 +1,17 @@
/**
* A dictionary for all of the days of the week
*/
const dayDictionary: Array<string> = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
export {
dayDictionary
};

View File

@ -1 +1,2 @@
export type monthType = "single" | "double" | "short" | "full" export type monthType = "single" | "double" | "short" | "long";
export type dateType = "single" | "double" | "short" | "long" | "dowsingle" | "dowdouble";

View File

@ -1,5 +1,6 @@
import { dayDictionary } from "./Dictionaries/dateDictionary";
import { monthDictionary } from "./Dictionaries/monthDictionary"; import { monthDictionary } from "./Dictionaries/monthDictionary";
import { monthType } from "./Types/formatTypes"; import { dateType, monthType } from "./Types/formatTypes";
@ -30,7 +31,7 @@ export default class TymeJS {
* Get the currently assigned date object * Get the currently assigned date object
* @returns The assigned Date object * @returns The assigned Date object
*/ */
public getDate(): Date { public getDateObject(): Date {
return this.ts; return this.ts;
} }
@ -53,7 +54,7 @@ export default class TymeJS {
* Get the current month * Get the current month
*/ */
public getMonth(mType: monthType): string { public getMonth(mType: monthType): string {
// get the index of the current month
let monthIndex: number = this.ts.getMonth(); let monthIndex: number = this.ts.getMonth();
if ( mType === "single" ) { if ( mType === "single" ) {
@ -68,6 +69,27 @@ export default class TymeJS {
} }
public getDate(dType: dateType): string {
if ( dType == "single" ) {
// get the index of the current day
let currentDate: number = this.ts.getDate();
return String(currentDate);
} else if ( dType == "double" ) {
// get the index of the current day
let currentDate: number = this.ts.getDate();
return String(currentDate).padStart(2, "0");
} else if ( dType == 'dowsingle' ) {
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);
}
}
} }

18
tests/dateTest.ts Normal file
View File

@ -0,0 +1,18 @@
import TymeJS from "../src";
let startDate: 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"),
});
startDate.setDate(startDate.getDate() + 1);
}

View File

@ -10,7 +10,7 @@ for ( let i = 0; i < 12; i++ ) {
console.log({ console.log({
single: a.getMonth("single"), single: a.getMonth("single"),
double: a.getMonth("double"), double: a.getMonth("double"),
full: a.getMonth("full"), full: a.getMonth("long"),
short: a.getMonth("short"), short: a.getMonth("short"),
}); });
} }