⏱️ An anwsome and tiny util package for timestamp without dependencies
- timestamp-utils
- Navigation 🗺️ :
- Why 🤔
- How to use ✍️
- API 📖
- decompose(timestamp, [timezone='UTC'])
- getYear(timestamp)
- getMonth(timestamp)
- getWeekDay(timestamp)
- getDay(timestamp)
- getHours(timestamp)
- getMinutes(timestamp)
- getSeconds(timestamp)
- getMilliseconds(timestamp)
- addYears(timestamp, years)
- addMonths(timestamp, months)
- addDays(timestamp, days)
- addHours(timestamp, hours)
- addMinutes(timestamp, minutes)
- addSeconds(timestamp, seconds)
- addMilliseconds(timestamp, milliseconds)
- add(timestamp, values)
- setYear(timestamp, year)
- setMonth(timestamp, month)
- setWeekDay(timestamp, weekDay)
- setDay(timestamp, day)
- setHours(timestamp, hours)
- setMinutes(timestamp, minutes)
- setSeconds(timestamp, seconds)
- setMilliseconds(timestamp, millisecondes)
- set(timestamp, values)
- setTimezone(timezone)
- Changelog 📋
- Development 💻
- License 🖋
Because when you manipulate date with javascript Date
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.
Install timestamp-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)
- Return : Array of String
- Params :
- timestamp : Int (timestamp)
- timezone : String (since v2.0.0)
- Available since : v1.0.0
Decompose timestamp
to the following array pattern :
[year, month, day, hours, minutes, seconds, milliseconds]
- Return : String
- Params :
- timestamp : Int (timestamp)
- timezone : String (since v1.0.3)
- Available since : v1.0.0
Return the timestamp
's year.
- Return : String
- Params :
- timestamp : Int (timestamp)
- timezone : String (since v1.0.3)
- Available since : v1.0.0
Return the timestamp
's month (eg: "01" for "january").
- Return : Integer
- Params :
- timestamp : Int (timestamp)
- timezone : String (since v1.0.3)
- Available since : v1.0.3
Return the timestamp
's week day (eg: 0 for "monday").
- Return : String
- Params :
- timestamp : Int (timestamp)
- timezone : String (since v1.0.3)
- Available since : v1.0.0
Return the timestamp
's day (eg: "01" for "monday").
- Return : String
- Params :
- timestamp : Int (timestamp)
- timezone : String (since v1.0.3)
- Available since : v1.0.0
Return the timestamp
's hours.
- Return : String
- Params :
- timestamp : Int (timestamp)
- timezone : String (since v1.0.3)
- Available since : v1.0.0
Return the timestamp
's minutes.
- Return : String
- Params :
- timestamp : Int (timestamp)
- timezone : String (since v1.0.3)
- Available since : v1.0.0
Return the timestamp
's seconds.
- Return : String
- Params :
- timestamp : Int (timestamp)
- timezone : String (since v1.0.3)
- Available since : v1.0.0
Return the timestamp
's milliseconds.
- Return : Int (timestamp)
- Params :
- timestamp : Int (timestamp)
- years: Int (years to add)
- Available since : v1.0.0
Add the given years
to the given timestamp
.
years
can be negative to subtract years.
- Return : Int (timestamp)
- Params :
- timestamp : Int (timestamp)
- months: Int (months to add)
- 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)
- Return : Int (timestamp)
- Params :
- timestamp : Int (timestamp)
- days: Int (days to add)
- Available since : v1.0.0
Add the given days
to the given timestamp
.
days
can be negative to subtract days.
- Return : Int (timestamp)
- Params :
- timestamp : Int (timestamp)
- hours: Int (hours to add)
- Available since : v1.0.0
Add the given hours
to the given timestamp
.
hours
can be negative to subtract hours.
- Return : Int (timestamp)
- Params :
- timestamp : Int (timestamp)
- minutes: Int (minutes to add)
- Available since : v1.0.0
Add the given minutes
to the given timestamp
.
minutes
can be negative to subtract minutes.
- Return : Int (timestamp)
- Params :
- timestamp : Int (timestamp)
- seconds: Int (seconds to add)
- Available since : v1.0.0
Add the given seconds
to the given timestamp
.
seconds
can be negative to subtract seconds.
- Return : Int (timestamp)
- Params :
- timestamp : Int (timestamp)
- milliseconds: Int (milliseconds to add)
- Available since : v1.0.0
Add the given milliseconds
to the given timestamp
.
milliseconds
can be negative to subtract milliseconds.
- Return : Int (timestamp)
- Params :
- timestamp : Int (timestamp)
- values: Object
- 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
- Return : Int (timestamp)
- Params :
- timestamp : Int (timestamp)
- year: Int (year to set)
- Available since : v2.1.0
Set the given year
to the given timestamp
.
- Return : Int (timestamp)
- Params :
- timestamp : Int (timestamp)
- month: Int (month to set)
- Available since : v2.1.0
Set the given month
to the given timestamp
.
- Return : Int (timestamp)
- Params :
- timestamp : Int (timestamp)
- weekDay: Int (weekDay to set)
- 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)
- Return : Int (timestamp)
- Params :
- timestamp : Int (timestamp)
- day: Int (day to set)
- Available since : v2.1.0
Set the given day
to the given timestamp
.
- Return : Int (timestamp)
- Params :
- timestamp : Int (timestamp)
- hours: Int (hours to set)
- Available since : v2.1.0
Set the given hours
to the given timestamp
.
- Return : Int (timestamp)
- Params :
- timestamp : Int (timestamp)
- minutes: Int (minutes to set)
- Available since : v2.1.0
Set the given minutes
to the given timestamp
.
- Return : Int (timestamp)
- Params :
- timestamp : Int (timestamp)
- seconds: Int (seconds to set)
- Available since : v2.1.0
Set the given seconds
to the given timestamp
.
- Return : Int (timestamp)
- Params :
- timestamp : Int (timestamp)
- millisecondes: Int (millisecondes to set)
- Available since : v2.1.0
Set the given millisecondes
to the given timestamp
.
- Return : Int (timestamp)
- Params :
- timestamp : Int (timestamp)
- values: Object
- 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
.
- Return : void
- Params :
- timezone : String
- Available since : v2.0.0
Set the global timezone
that timestamp-utils
should use.
- Update some dependencies #180.
- Enable
0
value forset
method #125 #181. - Fix
timezoneOffset
util date parsing #183.
- Adding
set
methods :
- Fix getWeekDay usage of timezone.
- Fix decompose slow execution speed.
- 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')
// Clone the project
git clone [email protected]: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