1.0.13 • Published 6 years ago
@ecromaneli/clockjs v1.0.13
Install
npm i @ecromaneli/clockjsConstructors
Import script
To use ClockJS, import script with this code:
const {ClockJS} = require('@ecromaneli/clockjs')Now
To get instance of ClockJS with actual date, use:
ClockJS.now(): ClockJSFrom timestamp
To get instance of ClockJS with timestamp in milliseconds (like Date.now()), use:
ClockJS.fromTimestamp(timestamp: number): ClockJSPublic Methods
start(): voidstart ticking;stop(): voidstop ticking;alreadyStarted(): booleanreturn if clock is already started;tick(): voidmanually tick.
Events
tickingon clock tick;yearon year change;monthon month change;dayon day change;houron hour change;minuteon minute change;secondon second change.
Bind and Unbind handlers
.on (events: string, handler: (event: string, clock: ClockJS) => void)
.off(events: string, handler: (event: string, clock: ClockJS) => void)Format date
You can format the clock time any way you want by using a string with:
Yyear, 1 - 4 digits;Mmonth number, 1 or 2 digits;Wweekday number, 1 or 2 digits;Dday, 1 or 2 digits;Hhours (24 hours), 1 or 2 digits;hhours, 1 or 2 digits;mminutes, 1 or 2 digits;sseconds, 1 or 2 digits;umilliseconds in seconds, 1 digit (.000);TZDtimezone, +-HH:mm or Z;MMMorMONmonth abbrev. (Jan, Feb, Mar...);MONTHmonth name (January, February...);WWWweekday abbrev. (Mon, Tue, Wed...);WEEKDAYweekday name (Monday, Tuesday...).
The function:
    // Print something like 2018 Jun 04, 15:49:32.770
    .format('YYYY MON DD, hh:mm:ss.u')For example:
The ISO 8601 format is: 2018-06-04T05:57:23.557Z.
    // ISO 8601
    clock.format('YYYY-MM-DDThh:mm:ss.uTZD')Usage Example
const {ClockJS} = require('@ecromaneli/clockjs')
let clock = ClockJS.now()
let handler = (event, clock) => {
    console.log(clock.format('YYYY-MM-DD hh:mm:ss'))
};
// Start clock (now, clock ticking)
clock.start()
// Bind handler on year, day and second change
clock.on('year day second', handler)
// Unbind handler on year and day change
clock.off('year day', handler)
// Stop clock
clock.stop()Obs.: You don't need start the clock to use date information or .format() method. You need start, to use listeners.
Getters (ES5 Format)
Year
year: numbercurrent year;yearFloat: numberyear float value.
Month
month: numbercurrent month (0 - 11);monthFloat: numbermonth float value;monthName: stringenglish month name.
Week
weekday: numbercurrent weekday (0 - 6);weekdayFloat: numberweekday float value;weekdayName: stringenglish weekday name.
Day
lastDayOfMonth: numberlast day of current month;day: numbercurrent day;dayFloat: numberday float value.
Hours
hours: numbercurrent hours;hoursFloat: numberhours float value.
Minutes
minutes: numbercurrent minutes;minutesFloat: numberminutes float value.
Seconds
seconds: numbercurrent seconds;secondsFloat: numberseconds float value.
Milliseconds
milliseconds: numbermilliseconds.
Others
dateTime: stringstring of current time in DateTime format;timezone: stringcurrent timezone. ( +-hh:mm|Z )
Getters & Setters (ES5 Format)
speed: numberclock speed 0 - 59 (default: 1);timestamp: numbercurrent timestamp;clockTick: numbercurrent clocktick time in ms 10 - 1000.
Example
let clock = ClockJS.fromTimestamp(0);
console.log(clock.timestamp); // GET (print 0)
clock.timestamp = Date.now(); // SET (update actual timestamp)Author
- Created and maintained by Emerson C. Romaneli (@ECRomaneli).