timestamp-utils
β±οΈ An anwsome and tiny util package for timestamp without dependenciesNavigation πΊοΈ :
- Navigation πΊοΈ : - Why π€ - How to use βοΈ - API π
- [decompose(timestamp, [timezone='UTC'])](#decomposetimestamp-timezoneutc)
- [getYear(timestamp)](#getyeartimestamp)
- [getMonth(timestamp)](#getmonthtimestamp)
- [getWeekDay(timestamp)](#getweekdaytimestamp)
- [getDay(timestamp)](#getdaytimestamp)
- [getHours(timestamp)](#gethourstimestamp)
- [getMinutes(timestamp)](#getminutestimestamp)
- [getSeconds(timestamp)](#getsecondstimestamp)
- [getMilliseconds(timestamp)](#getmillisecondstimestamp)
- [addYears(timestamp, years)](#addyearstimestamp-years)
- [addMonths(timestamp, months)](#addmonthstimestamp-months)
- [addDays(timestamp, days)](#adddaystimestamp-days)
- [addHours(timestamp, hours)](#addhourstimestamp-hours)
- [addMinutes(timestamp, minutes)](#addminutestimestamp-minutes)
- [addSeconds(timestamp, seconds)](#addsecondstimestamp-seconds)
- [addMilliseconds(timestamp, milliseconds)](#addmillisecondstimestamp-milliseconds)
- [add(timestamp, values)](#addtimestamp-values)
- [setYear(timestamp, year)](#setyeartimestamp-year)
- [setMonth(timestamp, month)](#setmonthtimestamp-month)
- [setWeekDay(timestamp, weekDay)](#setweekdaytimestamp-weekday)
- [setDay(timestamp, day)](#setdaytimestamp-day)
- [setHours(timestamp, hours)](#sethourstimestamp-hours)
- [setMinutes(timestamp, minutes)](#setminutestimestamp-minutes)
- [setSeconds(timestamp, seconds)](#setsecondstimestamp-seconds)
- [setMilliseconds(timestamp, millisecondes)](#setmillisecondstimestamp-millisecondes)
- [set(timestamp, values)](#settimestamp-values)
- [setTimezone(timezone)](#settimezonetimezone)
- Changelog π- [v2.2.0](#v220)
- [v2.1.0](#v210)
- [v2.0.2](#v202)
- [v2.0.1](#v201)
- [v2.0.0](#v200)
- Development π»
- License πWhy π€
Because when you manipulate date with javascriptDate
class it automatically apply the current timezone.
Using timestamp is a good way to avoid timezones's influences.
But using timestamp for huge maninupulations can be very hard (ex: go to next months).That why i created
timestamp-utils
, it's a powerful util package to easly manipulate timestamp.How to use βοΈ
Installtimestamp-utils
via npm :npm install --save timestamp-utils
Or via yarn :
yarn add timestamp-utils
Use it :
import t from 'timestamp-utils'
const now = new Date().getTime()
const timestamp = t.addDays(now, 3)
API π
decompose(timestamp, timezone='UTC')
- Return : Array of String
- Params :
- Available since : v1.0.0
Decompose
timestamp
to the following array pattern :
[year, month, day, hours, minutes, seconds, milliseconds]
getYear(timestamp)
- Return : String
- Params :
- Available since : v1.0.0
Return the
timestamp
's year.getMonth(timestamp)
- Return : String
- Params :
- Available since : v1.0.0
Return the
timestamp
's month (eg: "01" for "january").getWeekDay(timestamp)
- Return : Integer
- Params :
- Available since : v1.0.3
Return the
timestamp
's week day (eg: 0 for "monday").getDay(timestamp)
- Return : String
- Params :
- Available since : v1.0.0
Return the
timestamp
's day (eg: "01" for "monday").getHours(timestamp)
- Return : String
- Params :
- Available since : v1.0.0
Return the
timestamp
's hours.getMinutes(timestamp)
- Return : String
- Params :
- Available since : v1.0.0
Return the
timestamp
's minutes.getSeconds(timestamp)
- Return : String
- Params :
- Available since : v1.0.0
Return the
timestamp
's seconds.getMilliseconds(timestamp)
- Return : String
- Params :
- Available since : v1.0.0
Return the
timestamp
's milliseconds.addYears(timestamp, years)
- Return : Int (timestamp)
- Params :
- Available since : v1.0.0
Add the given
years
to the given timestamp
.
years
can be negative to subtract years.addMonths(timestamp, months)
- Return : Int (timestamp)
- Params :
- Available since : v1.0.0
Add the given
months
to the given timestamp
.
months
can be negative to subtract months.β οΈ Note :addMonths
doesn't add same amont of day.
addMonths
add days depends on the given day, the result is always the nearest month's day that the given month's day :
09 October + 1 month => 09 November (+31 days) 31 August + 1 month => 30 September (+30 days) 31 January 2018 + 1 month => 28 February (+28 days)
addDays(timestamp, days)
- Return : Int (timestamp)
- Params :
- Available since : v1.0.0
Add the given
days
to the given timestamp
.
days
can be negative to subtract days.addHours(timestamp, hours)
- Return : Int (timestamp)
- Params :
- Available since : v1.0.0
Add the given
hours
to the given timestamp
.
hours
can be negative to subtract hours.addMinutes(timestamp, minutes)
- Return : Int (timestamp)
- Params :
- Available since : v1.0.0
Add the given
minutes
to the given timestamp
.
minutes
can be negative to subtract minutes.addSeconds(timestamp, seconds)
- Return : Int (timestamp)
- Params :
- Available since : v1.0.0
Add the given
seconds
to the given timestamp
.
seconds
can be negative to subtract seconds.addMilliseconds(timestamp, milliseconds)
- Return : Int (timestamp)
- Params :
- Available since : v1.0.0
Add the given
milliseconds
to the given timestamp
.
milliseconds
can be negative to subtract milliseconds.add(timestamp, values)
- Return : Int (timestamp)
- Params :
- Available since : v1.0.0
add
is a combo of all previous "add" methods.
values
is an object that follow this pattern :
{ years, months, days, hours, minutes, seconds, milliseconds }
All values
values are int
and represent the key
value to add.Example :
{ years: 3, days: -1, seconds: 20 }
will add 3 years, subtract 1 days and add 20 seconds to the given timestamp.β οΈ Note :add
callsaddMilliseconds
,addSeconds
,addMinutes
,addHours
,addDays
,addMonths
andaddYears
in this order.
That mean, according to addMonths's note,add(t, { days: -1, months: -1 })
andaddDays(addMonths(t, -1), -1)
are not always equals.
Example :add(30 March 2018, { days: -1, months: -1 })
=>28 February 2018
,addDays(addMonths(30 March 2018, -1), -1)
=>27 February 2018
setYear(timestamp, year)
- Return : Int (timestamp)
- Params :
- Available since : v2.1.0
Set the given
year
to the given timestamp
.setMonth(timestamp, month)
- Return : Int (timestamp)
- Params :
- Available since : v2.1.0
Set the given
month
to the given timestamp
.setWeekDay(timestamp, weekDay)
- Return : Int (timestamp)
- Params :
- Available since : v2.1.0
Set the given
weekDay
to the given timestamp
.β οΈ Note : weekDay
must be an integer between 0 and 6 (0 for Monday, 6 for Sunday)
setDay(timestamp, day)
- Return : Int (timestamp)
- Params :
- Available since : v2.1.0
Set the given
day
to the given timestamp
.setHours(timestamp, hours)
- Return : Int (timestamp)
- Params :
- Available since : v2.1.0
Set the given
hours
to the given timestamp
.setMinutes(timestamp, minutes)
- Return : Int (timestamp)
- Params :
- Available since : v2.1.0
Set the given
minutes
to the given timestamp
.setSeconds(timestamp, seconds)
- Return : Int (timestamp)
- Params :
- Available since : v2.1.0
Set the given
seconds
to the given timestamp
.setMilliseconds(timestamp, millisecondes)
- Return : Int (timestamp)
- Params :
- Available since : v2.1.0
Set the given
millisecondes
to the given timestamp
.set(timestamp, values)
- Return : Int (timestamp)
- Params :
- Available since : v1.0.0
set
is a combo of all previous "set" methods.
values
is an object that follow this pattern :
{ year, month, day, hours, minutes, seconds, milliseconds }
All values
values are int
and represent the key
value to add.Example :
{ year: 1992, days: 9, seconds: 14 }
will set year to 1992, day to 9 and seconds to 14 to the given timestamp
.setTimezone(timezone)
- Return : void
- Params :
- Available since : v2.0.0
Set the global
timezone
that timestamp-utils
should use.Changelog π
v2.2.0
- Update some dependencies #180.
- Enable
0
value forset
method #125 #181. - Fix
timezoneOffset
util date parsing #183.
v2.1.0
- Adding
set
methods :
v2.0.2
- Fix getWeekDay usage of timezone.
v2.0.1
- Fix decompose slow execution speed.
v2.0.0
- decompose is no longer accessible by using deconstructing import. Now decompose is accessible by doing :
import t from 'timestamp-utils'
const results = t.decompose(now)
- decompose now support timezone :
import t from 'timestamp-utils'
const results = t.decompose(now, 'Europe/Paris')
- New method
setTimezone
:
import t from 'timestamp-utils'
t.setTimezone('Europe/Paris')
Development π»
// Clone the project
git clone git@github.com:lelivrescolaire/react-light-calendar.git
// β¬οΈ Install node modules
npm install
// π Start the project
npm run watch
// β
Run tests
npm run test
// ποΈ Build the project
npm run build
// π Keep an eye on the bundle size
npm run size