Compare commits
26 Commits
9429bfa374
...
master
Author | SHA1 | Date |
---|---|---|
Daniel Legt | c0f43b21af | |
Daniel Legt | 3c63b9cc5b | |
Daniel Legt | 5eac2dc1e2 | |
Daniel Legt | 2fca2d3f18 | |
Daniel Legt | 3b935ba99e | |
Daniel Legt | 8ca22248c1 | |
Daniel Legt | d2f72cdb6e | |
Daniel Legt | c7694ad02c | |
Daniel Legt | f32ac70832 | |
Daniel Legt | 2e55071179 | |
Daniel Legt | 3a9ccf83b5 | |
Daniel Legt | dc77be9a2d | |
Daniel Legt | 6ce817ab1b | |
Daniel Legt | afe37e1f78 | |
Daniel Legt | 04dc772e05 | |
Daniel Legt | 5b71124dde | |
Daniel Legt | 3cd34ad28a | |
Daniel Legt | 89105840cc | |
Daniel Legt | 48a6d18c24 | |
Daniel Legt | 97aa3e2281 | |
Daniel Legt | d97a80bb39 | |
Daniel Legt | 8d3de497e9 | |
Daniel Legt | 91c4a86757 | |
Daniel Legt | a88351922f | |
Daniel Legt | c596812d30 | |
Daniel Legt | 1112a92023 |
|
@ -1,5 +1,5 @@
|
||||||
node_modules
|
node_modules
|
||||||
package-lock.json
|
lib
|
||||||
|
|
||||||
dist/*
|
dist/*
|
||||||
!dist.keep
|
!dist/.keep
|
|
@ -0,0 +1,2 @@
|
||||||
|
src
|
||||||
|
tsconfig.json
|
91
README.md
91
README.md
|
@ -1 +1,90 @@
|
||||||
# Tyme.js
|
![Tyme.JS](./logo.png)
|
||||||
|
|
||||||
|
[Site](https://justkato.me/) |
|
||||||
|
[Docs/Wiki](https://github.com/JustKato/Tyme.js/wiki) |
|
||||||
|
[Contributing](CONTRIBUTING.md) |
|
||||||
|
|
||||||
|
Building the project yourself:
|
||||||
|
```shell
|
||||||
|
$ git pull https://github.com/JustKato/Tyme.js tymejs
|
||||||
|
$ cd tymejs
|
||||||
|
$ npm run build
|
||||||
|
```
|
||||||
|
|
||||||
|
## Download
|
||||||
|
|
||||||
|
[![Web Download](https://img.shields.io/badge/Web-Download%20Package-yellow?style=for-the-badge&logo=javascript)](https://github.com/JustKato/Tyme.js/releases/download/3.0.0/tyme.js-3.0.0.zip.zip)
|
||||||
|
[![NPM Download](https://img.shields.io/badge/NPM-Download-orange?style=for-the-badge&logo=npm)](https://www.npmjs.com/package/tymejs)
|
||||||
|
|
||||||
|
|
||||||
|
Tyme.js is released under the [MIT license](license) & supports modern environments.<br>
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
In a browser:
|
||||||
|
```html
|
||||||
|
<script src="tyme.js"></script>
|
||||||
|
```
|
||||||
|
|
||||||
|
Using npm:
|
||||||
|
```shell
|
||||||
|
$ npm i tymejs
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
All of the available formats are directly from [PHP's date formats](https://www.php.net/manual/en/datetime.format.php#refsect1-datetime.format-parameters)
|
||||||
|
|
||||||
|
|
||||||
|
#### ⚠ Warning ⚠
|
||||||
|
⚠ Not all different character formats have been implemented. ⚠
|
||||||
|
|
||||||
|
|
||||||
|
In Node.js TypeScript:
|
||||||
|
```js
|
||||||
|
// Import the Package
|
||||||
|
import TymeJS from "tymejs"; // Use require for plain javascript
|
||||||
|
|
||||||
|
// Get the date object from Somewhere
|
||||||
|
let myDate: Date = new Date();
|
||||||
|
|
||||||
|
// Initialize the TymeJS Object
|
||||||
|
let t: TymeJS = new TymeJS(myDate);
|
||||||
|
|
||||||
|
// Now you can format different dates
|
||||||
|
let formattedDate: string = t.format("d/m/Y H:i A");
|
||||||
|
|
||||||
|
// All Available formats: https://www.php.net/manual/en/datetime.format.php#refsect1-datetime.format-parameters
|
||||||
|
|
||||||
|
// Log the formatted Date
|
||||||
|
console.log(formattedDate);
|
||||||
|
```
|
||||||
|
|
||||||
|
In Node.js using JavaScript:
|
||||||
|
```js
|
||||||
|
const { default: TymeJS } = require("tymejs");
|
||||||
|
|
||||||
|
// Get the date object from Somewhere
|
||||||
|
let myDate = new Date();
|
||||||
|
|
||||||
|
// Initialize the TymeJS Object
|
||||||
|
let t = new TymeJS(myDate);
|
||||||
|
|
||||||
|
// Now you can format different dates
|
||||||
|
let formattedDate = t.format("d/m/Y H:i A");
|
||||||
|
|
||||||
|
// All Available formats: https://www.php.net/manual/en/datetime.format.php#refsect1-datetime.format-parameters
|
||||||
|
|
||||||
|
// Log the formatted Date
|
||||||
|
console.log(formattedDate);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Why TymeJS?
|
||||||
|
|
||||||
|
TymeJS makes working with JavaScript date objects much easier, I've found myself<br>
|
||||||
|
multiple times in the situation where the client doesn't like their region's date format
|
||||||
|
and requests for custom formats, this consumed a lot of time implementing, so that's why
|
||||||
|
I have created this library, for:
|
||||||
|
* Reliable Date formats
|
||||||
|
* Reliable Time formats
|
||||||
|
* TypeScript Support
|
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"transform": {
|
||||||
|
"^.+\\.(t|j)sx?$": "ts-jest"
|
||||||
|
},
|
||||||
|
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
|
||||||
|
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"]
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) [2022] [Kato Twofold]
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
File diff suppressed because it is too large
Load Diff
30
package.json
30
package.json
|
@ -1,18 +1,36 @@
|
||||||
{
|
{
|
||||||
"name": "tymejs",
|
"name": "tymejs",
|
||||||
"version": "0.1.0",
|
"version": "0.4.5",
|
||||||
"description": "Quickly and easily format JavaScript date Objects",
|
"description": "Quickly and easily format JavaScript date Objects",
|
||||||
"main": "dist/index.js",
|
"main": "lib/index.js",
|
||||||
|
"types": "lib/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "esbuild src/index.ts --bundle --minify --sourcemap --target=chrome58,firefox57,safari11,edge16 --outfile=dist/tyme.js",
|
"build": "tsc",
|
||||||
"test": "tsc"
|
"buildweb": "esbuild src/index.ts --bundle --minify --sourcemap --target=chrome58,firefox57,safari11,edge16 --outfile=dist/tyme.js",
|
||||||
|
"test": "jest --config jestconfig.json"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"time",
|
"time",
|
||||||
|
"time format",
|
||||||
"date",
|
"date",
|
||||||
"date",
|
"date format",
|
||||||
|
"datetime",
|
||||||
|
"datetime format",
|
||||||
"format"
|
"format"
|
||||||
],
|
],
|
||||||
"author": "Kato Twofold",
|
"author": "Kato Twofold",
|
||||||
"license": "MIT"
|
"license": "MIT",
|
||||||
|
"repository": {
|
||||||
|
"url": "https://github.com/JustKato/Tyme.js"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/jest": "^27.5.0",
|
||||||
|
"jest": "^28.1.0",
|
||||||
|
"ts-jest": "^28.0.1",
|
||||||
|
"typescript": "^4.6.4",
|
||||||
|
"ts-node": "^10.7.0"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"lib/**/*"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
};
|
|
@ -1 +1,67 @@
|
||||||
export type monthType = "single" | "double" | "short" | "full"
|
// All mappings according to: https://www.php.net/manual/en/datetime.format.php#refsect1-datetime.format-parameters
|
||||||
|
|
||||||
|
// ############################## [ 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 ] ##############################
|
||||||
|
|
||||||
|
// =-=-=-=-=-=-=-=-=-=-= [ Meridiem ] =-=-=-=-=-=-=-=-=-=-=
|
||||||
|
const mertypes = [ "a", "A" ];
|
||||||
|
/**
|
||||||
|
* Possible formats for the date
|
||||||
|
*/
|
||||||
|
export type meridiemType = "a" | "A";
|
||||||
|
export const isMeridiemType = (x: any): x is meridiemType => mertypes.includes(x);
|
||||||
|
|
||||||
|
// =-=-=-=-=-=-=-=-=-=-= [ Hours ] =-=-=-=-=-=-=-=-=-=-=
|
||||||
|
const hourtypes = [ "g", "G", "h", "H" ];
|
||||||
|
/**
|
||||||
|
* Possible formats for the date
|
||||||
|
*/
|
||||||
|
export type hourType = "g" | "G" | "h" | "H";
|
||||||
|
export const isHourType = (x: any): x is hourType => hourtypes.includes(x);
|
||||||
|
|
||||||
|
// =-=-=-=-=-=-=-=-=-=-= [ Minutes ] =-=-=-=-=-=-=-=-=-=-=
|
||||||
|
const minutetypes = [ "i" ];
|
||||||
|
/**
|
||||||
|
* Possible formats for the date
|
||||||
|
*/
|
||||||
|
export type minuteType = "i";
|
||||||
|
export const isMinuteType = (x: any): x is minuteType => minutetypes.includes(x);
|
||||||
|
|
||||||
|
// =-=-=-=-=-=-=-=-=-=-= [ Seconds ] =-=-=-=-=-=-=-=-=-=-=
|
||||||
|
const secondtypes = [ "s" ];
|
||||||
|
/**
|
||||||
|
* Possible formats for the date
|
||||||
|
*/
|
||||||
|
export type secondType = "s";
|
||||||
|
export const isSecondType = (x: any): x is secondType => secondtypes.includes(x);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// ############################## [ Date Formats ] ##############################
|
||||||
|
|
||||||
|
// =-=-=-=-=-=-=-=-=-=-= [ Date ] =-=-=-=-=-=-=-=-=-=-=
|
||||||
|
const dtypes = [ "j", "d", "D", "l", "w" ];
|
||||||
|
/**
|
||||||
|
* Possible formats for the date
|
||||||
|
*/
|
||||||
|
export type dateType = "j" | "d" | "D" | "l" | "w";
|
||||||
|
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 = "n" | "m" | "M" | "F";
|
||||||
|
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 = "L" | "o" | "y" | "Y";
|
||||||
|
export const isYearType = (x: any): x is yearType => yeartypes.includes(x);
|
|
@ -0,0 +1,14 @@
|
||||||
|
import TymeJS from "../index";
|
||||||
|
|
||||||
|
test(`Formatting Test`, () => {
|
||||||
|
let ts = new Date();
|
||||||
|
|
||||||
|
ts.setFullYear(1998);
|
||||||
|
ts.setMonth(9);
|
||||||
|
ts.setDate(24);
|
||||||
|
|
||||||
|
let ty = new TymeJS(ts);
|
||||||
|
|
||||||
|
expect(ty.format("d/m/Y")).toBe("24/10/1998")
|
||||||
|
|
||||||
|
})
|
174
src/index.ts
174
src/index.ts
|
@ -1,12 +1,14 @@
|
||||||
|
import { dayDictionary } from "./Dictionaries/dateDictionary";
|
||||||
import { monthDictionary } from "./Dictionaries/monthDictionary";
|
import { monthDictionary } from "./Dictionaries/monthDictionary";
|
||||||
import { monthType } from "./Types/formatTypes";
|
import { commonDateFormats, dateType, hourType, isDateType, isHourType, isMeridiemType, isMinuteType, isMonthType, isSecondType, isYearType, meridiemType, minuteType, monthType, secondType, yearType } from "./Types/formatTypes";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The main Tyme Class, initialize it by optionally passing a date object
|
* The main Tyme Class, initialize it by optionally passing a date object
|
||||||
|
* Created by Kato Twofold https://justkato.me
|
||||||
|
* @author Kato Twofold
|
||||||
|
* @example new TymeJS().format("d/m/Y h:i A")
|
||||||
|
* @class
|
||||||
*/
|
*/
|
||||||
export default class TymeJS {
|
export default class TymeJS {
|
||||||
|
|
||||||
|
@ -30,42 +32,184 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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: commonDateFormats | string ): 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) ) {
|
||||||
|
// 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 if ( isMeridiemType(char) )
|
||||||
|
formattedDate += this.getMeridiem(char);
|
||||||
|
else if ( isHourType(char) )
|
||||||
|
formattedDate += this.getHour(char);
|
||||||
|
else if ( isMinuteType(char) )
|
||||||
|
formattedDate += this.getMinutes(char);
|
||||||
|
else if ( isSecondType(char) )
|
||||||
|
formattedDate += this.getSeconds(char);
|
||||||
|
else {
|
||||||
|
// Just append the char to the result
|
||||||
|
formattedDate += char;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// Return the formatted date
|
// Return the formatted date
|
||||||
return formattedDate;
|
return formattedDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the current month
|
|
||||||
*/
|
|
||||||
public getMonth(mType: monthType): string {
|
|
||||||
|
|
||||||
|
// ############################## [ Time Formats ] ##############################
|
||||||
|
|
||||||
|
public getMeridiem(t: meridiemType): string {
|
||||||
|
// Get the current meridiem
|
||||||
|
let meridiem = ((this.ts.getHours() + 11) % 12 + 1) > 12 ? "pm" : "am";
|
||||||
|
// Check which format to return
|
||||||
|
if ( t === "A" ) {
|
||||||
|
return meridiem.toUpperCase();
|
||||||
|
} else if ( t === "a" ) {
|
||||||
|
return meridiem;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`Invalid format for meridiem ${t}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getHour(t: hourType): string | number {
|
||||||
|
// Get current hour
|
||||||
|
let h = this.ts.getHours();
|
||||||
|
|
||||||
|
if ( t == "G" ) {
|
||||||
|
return h;
|
||||||
|
} else if ( t === "g" ) {
|
||||||
|
if ( h > 12 )
|
||||||
|
return h - 12;
|
||||||
|
return h;
|
||||||
|
} else if ( t === "H" ) {
|
||||||
|
return String(h).padStart(2, "0");
|
||||||
|
} else if ( t === "h" ) {
|
||||||
|
if ( h > 12 )
|
||||||
|
return String(h - 12).padStart(2, "0");
|
||||||
|
return String(h).padStart(2, "0");
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`Invalid format for Hours ${t}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getMinutes(t: minuteType ): string {
|
||||||
|
|
||||||
|
if ( t === "i" ) {
|
||||||
|
return String(this.ts.getMinutes()).padStart(2, "0");
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`Invalid format for Minutes ${t}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getSeconds(t: secondType) {
|
||||||
|
if ( t === "s" ) {
|
||||||
|
return String(this.ts.getSeconds()).padStart(2, "0");
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`Invalid format for Seconds ${t}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ############################## [ Date Formats ] ##############################
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param t The format to get the date in
|
||||||
|
* @returns A string of the formatted date
|
||||||
|
*/
|
||||||
|
public getDate(t: dateType): string | number {
|
||||||
|
|
||||||
|
if ( t == "j" ) {
|
||||||
|
// get the index of the current day
|
||||||
|
let currentDate: number = this.ts.getDate();
|
||||||
|
return currentDate;
|
||||||
|
} else if ( t == "d" ) {
|
||||||
|
// get the index of the current day
|
||||||
|
let currentDate: number = this.ts.getDate();
|
||||||
|
return String(currentDate).padStart(2, "0");
|
||||||
|
} else if ( t == 'w' ) {
|
||||||
|
return String(this.ts.getDay());
|
||||||
|
} else if ( t == "l") {
|
||||||
|
return String(dayDictionary[this.ts.getDay()]);
|
||||||
|
} else if ( t == 'D' ) {
|
||||||
|
return String(dayDictionary[this.ts.getDay()]).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();
|
let monthIndex: number = this.ts.getMonth();
|
||||||
|
|
||||||
if ( mType === "single" ) {
|
if ( t === "n" ) {
|
||||||
return String(monthIndex + 1);
|
return (monthIndex + 1);
|
||||||
} else if ( mType === "double" ) {
|
} else if ( t === "m" ) {
|
||||||
return String(monthIndex + 1).padStart(2, "0");
|
return String(monthIndex + 1).padStart(2, "0");
|
||||||
} else if ( mType === "short" ) {
|
} else if ( t === "M" ) {
|
||||||
return monthDictionary[monthIndex].slice(0, 3);
|
return monthDictionary[monthIndex].slice(0, 3);
|
||||||
} else {
|
} else if ( t === "F" ) {
|
||||||
return monthDictionary[monthIndex];
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
import TymeJS from "..";
|
||||||
|
|
||||||
|
let d: Date = new Date();
|
||||||
|
|
||||||
|
for ( let i = 0; i < 7; i++ ) {
|
||||||
|
|
||||||
|
const a = new TymeJS(d);
|
||||||
|
console.log(d, {
|
||||||
|
single: a.getDate("j"),
|
||||||
|
double: a.getDate("d"),
|
||||||
|
dowdouble: a.getDate("w"),
|
||||||
|
full: a.getDate("l"),
|
||||||
|
short: a.getDate("D"),
|
||||||
|
});
|
||||||
|
|
||||||
|
d.setDate(d.getDate() + 1);
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
import TymeJS from "../index";
|
||||||
|
|
||||||
|
const a = new TymeJS();
|
||||||
|
|
||||||
|
console.log("🌍", a.format("m/d/Y"));
|
||||||
|
console.log(a.format("d/m/Y"));
|
||||||
|
console.log(a.format("d/m/Y h:i A"));
|
||||||
|
console.log(a.format("d/m/Y H:i:s"));
|
||||||
|
|
||||||
|
console.log("\n🇺🇸\n", a.format("m/d/Y"));
|
||||||
|
console.log(a.format("m/d/Y"));
|
||||||
|
console.log(a.format("m/d/Y h:i A"));
|
||||||
|
console.log(a.format("m/d/Y H:i:s"));
|
||||||
|
|
||||||
|
console.log("\n🖥\n", a.format("m/d/Y"));
|
||||||
|
console.log(a.format("Y-m-d"));
|
||||||
|
console.log(a.format("Y-m-d H:i:s"));
|
||||||
|
|
||||||
|
console.log(`\n`);
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import TymeJS from "../src";
|
import TymeJS from "..";
|
||||||
|
|
||||||
for ( let i = 0; i < 12; i++ ) {
|
for ( let i = 0; i < 12; i++ ) {
|
||||||
|
|
||||||
|
@ -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("full"),
|
full: a.getMonth("F"),
|
||||||
short: a.getMonth("short"),
|
short: a.getMonth("M"),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,12 +42,12 @@
|
||||||
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */
|
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */
|
||||||
|
|
||||||
/* Emit */
|
/* Emit */
|
||||||
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
||||||
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
||||||
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
||||||
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
||||||
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
|
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
|
||||||
"outDir": "./dist/", /* Specify an output folder for all emitted files. */
|
"outDir": "./lib", /* Specify an output folder for all emitted files. */
|
||||||
// "removeComments": true, /* Disable emitting comments. */
|
// "removeComments": true, /* Disable emitting comments. */
|
||||||
// "noEmit": true, /* Disable emitting files from a compilation. */
|
// "noEmit": true, /* Disable emitting files from a compilation. */
|
||||||
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
||||||
|
@ -97,5 +97,7 @@
|
||||||
/* Completeness */
|
/* Completeness */
|
||||||
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
||||||
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
||||||
}
|
},
|
||||||
|
"include": ["src"],
|
||||||
|
"exclude": ["node_modules", "**/__tests__/*"]
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue