1.3.2 • Published 6 years ago

js-datetime v1.3.2

Weekly downloads
3
License
Apache License
Repository
-
Last release
6 years ago

js-datetime

A datetime module for formatting, manupilating and creating dates. You can also create a stopwatches, timers and alarms

USAGE

const date = require("js-datetime");

date.date("{W}, {D}{j} {M} {Y} {H}:{I}:{S}");
//creates a DateTime using the current time, and timezone
//the format is `{Weekday}, {Date}{th/st/nd/rd} {Month} {Full Year} {Hour}:{Minute}:{Second}`

date.date("{today} {now}", "-05:00");
//this makes a DateTime object with an hour setoff of 5 hours backwards
//the format is the same as above

date.timezoneFromOffset("-05:00");
//returns an array of all timezone abbriviations with -05:00 hour offset
//it would be ["EST", "CDT", "ACT", "COT", "CST", "EASST", "ECT", "PET"]

date.date("{now}", date.offsetFromTimezone("PST"));
//makes a DateTime with the format {H}:{I}:{S} or {G}:{I}:{S} {p} depending if it's 24 or 12 hour system

date.toggle24HourSystem();
//first calling returns false, meaning it uses 12 hour format.
//now `{now}` is {G}:{I}:{S} {p}

var now = new date.DateTime("{today} {timeExact}", null, 1529522139628);
//this makes a new DateTime with a custom time, this is Wednesday, June 20th 2018, 21:15:39:628 (the time this was written)
//the second argument (null) is the timezone, I ommitted it to make it use the current timezone (+02:00)

now.isLeapYear(); //tests if the current year is a leap year, the module exports this also as
date.isLeapYear(now.year);

Formatting Variables

{w} - The first 3 letters of the weekday, careful the week starts with Sunday!

{W} - The current weekday full print, careful the week starts with Sunday!

{f} - The weekday in a numerical print

{F} - The weekday in a numerical print with prepended 0

{r} - The week in the year

{R} - The week in the year with prepended 0

{d} - The day-of-the-month

{D} - the day-of-the-month with prepended 0

{j} - The date extension like th

{m} - The first 3 letters of the month

{M} - The full textual representation of the month

{n} - The numeric representation of the month

{N} - The numeric representation of the month with prepended 0

{y} - The current year in 2 digits

{Y} - The current year

{g} - The hour in 12-hour time

{G} - The hour in 12-hour time with prepended 0

{h} - The hour in 24-hour time

{H} - The hour in 24-hour time with prepended 0

{i} - The minute

{I} - The minute with prepended 0

{s} - The second

{S} - The second with prepended 0

{u} - The millisecond

{U} - The millisecond with prepended 0s

{p} - am or pm

{P} - AM or PM

{k} - Ante Meridiem or Post Meridiem

{T} - The Timezone abbreviation

{O} - The offset to GMT like +01:00

{L} - The time since the UNIX epoch (January 01, 1970 00:00:00 UTC.)

{today} - This automatically adds the following format: {W}, {M} {D}{j}, {Y}

{tommorow} - This adds the format of {today} and adds 1 day

{yesterday} - This adds the format of {today} and removes 1 day

{now} - This is a shortcut to use {H}:{I}:{S} or {G}:{I}:{S} {A} depending on configuration

{shortDate} - A shortcut to {n}/{d}/{y}

{longDate} - A shortcut to {W}, {M} {D}, {Y}

{timeExact} - A shortcut to {H}:{I}:{S}:{U}

{split} - A shortcut to {f}|{d}|{n}|{Y}|{h}|{i}|{s}|{u}|{L}, this is for splitting it into a string array by using DateTime.formatted.split('|') at best it's used alone, this is the same format used when you construct a DateTime object with a number array,

{iso} - The ISO standart for the date format (this is how a Date object in JavaScript is saved) using the format of {Y}-{M}-{D}T{H}:{I}:{S}Z this format is also returned using DateTime.toJSONString()

{utc} - The UTC standart keeping the value of {w}, {D} {m} {Y} {H}:{I}:{S} GMT {O} keep in mind that this should be used with a GMT offset of +00:00

{date} - This is the normal date format being {w} {m} {D} {Y}

{time} - This is the default time format being {H}:{I}:{S} GMT {O} ({T})

{datetime} - This is the defautl date & time format made up of {date} {time}

const date = require("js-datetime");
date.getVariables() //This returns an array with all of the above variables

Updating DateTime, Timers and Stopwatches

Another feature of this module is to make real-time updating Dates as well as making Timers and Stopwatches. Here's how it works:

var date = require("js-datetime");

var updating = date.date().changeToUpdating(1, 2);
//this adds 2 milliseconds every (1) millisecond

updating.changeRate("adding", 3, "+");
//this adds 3 to the adding rate, this means it adds 5 milliseconds each (1) millisecond

updating.changeRate("adding", 1, "-");
//this removes 1 off the adding rate again.

updating.changeRate("updating", 4, "=");
//this sets the rate to 4, so it currently adds 2 milliseconds each 4 milliseconds.

updating.changeRate("updating", 2, "-");

var time = new date.Stopwatch(updating);
//This generates a new stopwatch, using the UpdatingDateTime object (which extends DateTime)
//calling Stopwatch.getTime() gives a TimeOffset

time.end();
//running this (on a Stopwatch object) ends the running time and returns it's time

//supposedly we didn't end the stopwatch yet
var offset = time.end();
console.log(`The stopwatch has been running for ${offset.date} ${offset.hour}:${offset.minute} (${offset.time})`)

var timer = new date.Timer("readme", updating);
//calling this generates a Timer object from now to the time of updating
//once the current time hits updating.time it emits the event "end" and ends the interval.

timer.on("end", name => {
    console.log(`Timer ${name} has ended`);
})

var alarm = new date.Alarm("readme", updating, [0, 7, 6]);
//Creating a new Alarm object means that every time the time hits the hour and minute hits
//When it hits the hour + minute it emits the event "ringing" with the current date and name

alarm.on("ringing", (date, name) => {
    console.log(`Alarm ${name} is ringing!`)
})

License

Copyright 2018 Lilith Cassani

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

1.3.2

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.8

6 years ago

1.2.7

6 years ago

1.2.4

6 years ago

1.2.3

6 years ago

1.0.3

6 years ago

1.0.0

6 years ago