Giter Site home page Giter Site logo

thewisker / formatterjs Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 3.8 MB

A javascript time and date formatter compatible with ES5 and ES6

License: GNU General Public License v3.0

JavaScript 98.40% Shell 1.60%
date es5 es6 formatter javascript module time

formatterjs's Introduction

Javascript Date Formatter

Module that simplifies the formatting of dates

Installation

ES5

git clone https://github.com/TheWisker/FormatterJS
cd FormatterJS
cp -a ./out/ES5/classes/. ../destination/
cd ..
rm -fr FormatterJS

ES6

Node.js:

npm i @thewisker/formatterjs

Manually:

git clone https://github.com/TheWisker/FormatterJS
cd FormatterJS
cp -a ./out/ES6/module/. ../destination/
cd ..
rm -fr FormatterJS

This series of commands install the module to the destination folder. Use one or another depending on the ES version (5 or 6) with wich it will be executed.

Note:
· The ES6 version is up to 2 times faster.
· All installation scripts are located under the /scripts folder.

Importing

ES5

Just add a script tag refering to the Formatter file before any script tag that depends on it.

<script type="text/javascript" src="/destination/Formatter.js"></script>
<script type="text/javascript" src="/YourScript.js"></script>

ES6

With Node.js:

import {UniversalFormatter, DateFormatter, TimeFormatter} from "@thewisker/formatterjs";

Note: To run it with node.js name the file with the .mjs extension or add "type" : "module" to the package.json.

Or manually, just add a import statement targeting the Formatter file with the classes to import between the brackets.

import {UniversalFormatter, DateFormatter, TimeFormatter} from "/destination/Formatter.js";

Note: All import examples are located under the /examples folder.

Module Usage

Create a new instance of the desired formatter:
Formatter Constructor Description
UniversalFormatter (Format: string, UTC: boolean) Formats date and time altogether
DateFormatter (Format: string, UTC: boolean) Formats only date
TimeFormatter (Format: string, UTC: boolean) Formats only time

Then call the format function on the object and pass an optional date parameter.

Function Parameters Default Description
format (date: Date()) new Date() Formats the date object

Formats

All the avaible formats equivalencies.

Misc

Format Type Description Example
%% Escape Sequence Escapes the % character %

Date

Format Type Description Example
%Y Year The year 2022
%y Year The short year 22 or 022
%J Year The day of the year 364
%M Month The month number 02
%m Month The month number 2
%B Month The month name February
%b Month The month short name Feb
%D Day The day of the month 08
%d Day The day of the month 8
%A Weekday The name of the day Monday
%a Weekday The name of the day Mon
%W Weekday The day of the week 1

Time

Format Type Description Example
%H Hour The hour in 24 format 20
%h Hour The hour in 24 format 20
%I Hour The hour in 12 format 08
%i Hour The hour in 12 format 08
%K Minutes The minutes 06
%k Minutes The minutes 6
%S Seconds The seconds 04
%s Seconds The seconds 4
%L Decisecond The decisecond 2
%Q Centisecond The centiseconds 02
%q Centisecond The centiseconds 2
%F Milisecond The miliszeconds 06
%f Milisecond The miliszeconds 6
%P Timestamp The timestamp AM
%p Timestamp The timestamp am
%f Timezone Offset The timezone offset +02
%f Timezone Offset The timezone offset +2

Examples

var Formatter = new UniversalFormatter("%Y %H"); //Formats the year and hour.
console.log(Formatter.format()); //Prints to console the format for the current Date() object.

Output: 2020 12

var Formatter = new DateFormatter("%Y_%B"); //Formats the year and month.
console.log(Formatter.format(new Date("December 17, 1995 03:24:00"))); //Prints to console the format for the passed Date() object.

Output: 1995_December

var Formatter = new TimeFormatter("(%H-%S)"); //Formats the hour and second.
console.log(Formatter.format(new Date("November 20, 1998 03:25:00"))); //Prints to console the format for the passed Date() object.

Output: (03-25)

Note: All examples are located under the /examples folder.

Compatibility

Under the /out/{ES5, ES6}/function folders a Formatter.js files is located containing a single function format() that can be used when compatibility issues arise when using classes.

Function Parameters Default Description
format (date: Date(), format: string, utc: boolean) No defaults Formats the date object

Author

TheWisker

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.