@jmac18/epoch
v1.0.6
Published
A simple Javascript Date() extension for easier manipulation of dates
Downloads
7
Readme
Epoch
- Epoch extends Date() functionality with easy to use functions and Chai-like syntax
- No external dependencies
- Since this is just a wrapper around Date(), all leap year calculations are taken care of for you
Usage
// 2016-06-29T07:30:00.000Z
Epoch().now
// 2016-06-29T07:40:00.000Z
Epoch().minutes(10).from.now
// 2016-06-29T07:20:00.000Z
Epoch().minutes(10).ago
// 600000
Epoch().minutes(10).to.milliseconds
// 2016-06-30T11:50:00.000Z
Epoch().days(1).hours(4).minutes(20).from.now
// 2018-10-03T05:00:00.000Z
Epoch().years(2).months(3).days(4).from.date('6/29/2016')
API
Epoch()
Creates a Date
that defaults to the current date a time.
// Default
Epoch()
// With Optional Offset date
Epoch('6/29/2016') // or
Epoch(new Date(6/29/2016))
now
Returns the current date
Epoch().now
from
Used in conjunction with now
or date
to return a Date
from the specified date/time
// Return the date 1 day from now
Epoch().days(1).from.now
// Return the date 1 day from 6/29/2016
Epoch().days(1).from.date('6/29/2016')
date
Used in conjunction with from
to return a Date
from the specified date/time
// Defaults to the current date if no date is specified
Epoch().days(1).hours(4).from.date()
// You can use a string representation of the date
Epoch().days(1).from.date('6/29/2016')
// Or you can pass a Date() object
Epoch().days(1).hours(2).from.date(new Date('6/29/2016'))
ago
Returns the Date
from some time ago
// Return the date 1 day ago
Epoch().days(1).ago
// Return the date 1 day ago from 6/29/2016
Epoch('6/29/2016').days(1).ago
to
Used in conjunction with milliseconds
and seconds
to return the duration in milliseconds or seconds. See milliseconds
and seconds
for usage.
milliseconds
Returns the specified duration in milliseconds
// Return 10 minutes in milliseconds
Epoch().minutes(10).to.milliseconds
// Return 1 year, 2 days and 3 hours in milliseconds
Epoch().years(1).days(2).hours(3).to.milliseconds
NOTE: Months cannot be converted to milliseconds currently because of the varying days in a month. This functionality is planned for the future.
seconds
Returns the specified duration in seconds
// Return 10 minutes in seconds
Epoch().minutes(10).to.seconds
// Return 1 year, 2 days and 3 hours in seconds
Epoch().years(1).days(2).hours(3).to.seconds
NOTE: Months cannot be converted to seconds currently because of the varying days in a month. This functionality is planned for the future.
minutes
Specify the number of minutes to be used in the duration or date calculation. minutes
can be chained with other time functions. E.g. hours
days
weeks
months
years
See example below
// Return the date 10 minutes from now
Epoch().minutes(10).from.now
// Return 10 minutes in milliseconds
Epoch().minutes(10).to.milliseconds
// Return the date 2 days, 3 hours, and 24 minutes from now
Epoch().days(2).hours(3).minutes(24).from.now
hours
Specify the number of hours to be used in the duration or date calculation. hours
can be chained with other time functions. E.g. minutes
days
weeks
months
years
See example below
// Return the `Date` 10 hours from now
Epoch().hours(10).from.now
// Return 10 hours in milliseconds
Epoch().hours(10).to.milliseconds
// Return the `Date` 2 days, 3 hours, and 24 minutes from now
Epoch().days(2).hours(3).minutes(24).from.now
days
Specify the number of days to be used in the duration or date calculation. days
can be chained with other time functions. E.g. minutes
hours
weeks
months
years
See example below
// Return the `Date` 10 days from now
Epoch().days(10).from.now
// Return 10 days in milliseconds
Epoch().days(10).to.milliseconds
// Return the `Date` 2 days, 3 hours, and 24 minutes from now
Epoch().days(2).hours(3).minutes(24).from.now
weeks
Specify the number of weeks to be used in the duration or date calculation. weeks
can be chained with other time functions. E.g. minutes
hours
days
months
years
See example below
// Return the `Date` 10 weeks from now
Epoch().weeks(10).from.now
// Return 10 weeks in milliseconds
Epoch().weeks(10).to.milliseconds
// Return the `Date` 2 years, 3 months, and 22 weeks from now
Epoch().years(2).months(3).weeks(10).from.now
months
Specify the number of months to be used in the duration or date calculation. months
can be chained with other time functions. E.g. minutes
hours
days
weeks
years
See example below
// Return the `Date` 10 months from now
Epoch().months(10).from.now
// Return 10 months in milliseconds
Epoch().months(10).to.milliseconds
// Return the `Date` 2 years, 3 months, and 22 weeks from now
Epoch().years(2).months(3).weeks(10).from.now
years
Specify the number of years to be used in the duration or date calculation. years
can be chained with other time functions. E.g. minutes
hours
days
weeks
years
See example below
// Return the `Date` 10 years from now
Epoch().years(10).from.now
// Return 10 years in milliseconds
Epoch().years(10).to.milliseconds
// Return the `Date` 2 years, 3 months, and 22 weeks from now
Epoch().years(2).months(3).weeks(10).from.now
isLeapYear([year])
Takes a year and returns true
if it is a leap year and false
otherwise
// true
Epoch().isLeapYear(2000)
// false
Epoch().isLeapYear(2015)
// true
Epoch().isLeapYear(2016)