timestamp-utils v2.2.0
timestamp-utils
β±οΈ An anwsome and tiny util package for timestamp without dependencies
Navigation πΊοΈ :
- 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 π
Why π€
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.
How to use βοΈ
Install timestamp-utils via npm :
npm install --save timestamp-utilsOr via yarn :
yarn add timestamp-utilsUse 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 :
- 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]
getYear(timestamp)
- Return : String
- Params :
- timestamp : Int (timestamp)
- timezone : String (since v1.0.3)
- Available since : v1.0.0
Return the timestamp's year.
getMonth(timestamp)
- 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").
getWeekDay(timestamp)
- 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").
getDay(timestamp)
- 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").
getHours(timestamp)
- Return : String
- Params :
- timestamp : Int (timestamp)
- timezone : String (since v1.0.3)
- Available since : v1.0.0
Return the timestamp's hours.
getMinutes(timestamp)
- Return : String
- Params :
- timestamp : Int (timestamp)
- timezone : String (since v1.0.3)
- Available since : v1.0.0
Return the timestamp's minutes.
getSeconds(timestamp)
- Return : String
- Params :
- timestamp : Int (timestamp)
- timezone : String (since v1.0.3)
- Available since : v1.0.0
Return the timestamp's seconds.
getMilliseconds(timestamp)
- Return : String
- Params :
- timestamp : Int (timestamp)
- timezone : String (since v1.0.3)
- Available since : v1.0.0
Return the timestamp's milliseconds.
addYears(timestamp, years)
- 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.
addMonths(timestamp, months)
- 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 :
addMonthsdoesn't add same amont of day.addMonthsadd 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 :
- 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.
addHours(timestamp, hours)
- 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.
addMinutes(timestamp, minutes)
- 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.
addSeconds(timestamp, seconds)
- 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.
addMilliseconds(timestamp, milliseconds)
- 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.
add(timestamp, values)
- 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 :
addcallsaddMilliseconds,addSeconds,addMinutes,addHours,addDays,addMonthsandaddYearsin 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 :
- timestamp : Int (timestamp)
- year: Int (year to set)
- Available since : v2.1.0
Set the given year to the given timestamp.
setMonth(timestamp, month)
- 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.
setWeekDay(timestamp, weekDay)
- 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 :
weekDaymust be an integer between 0 and 6 (0 for Monday, 6 for Sunday)
setDay(timestamp, day)
- 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.
setHours(timestamp, hours)
- 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.
setMinutes(timestamp, minutes)
- 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.
setSeconds(timestamp, seconds)
- 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.
setMilliseconds(timestamp, millisecondes)
- 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.
set(timestamp, values)
- 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.
setTimezone(timezone)
- Return : void
- Params :
- timezone : String
- Available since : v2.0.0
Set the global timezone that timestamp-utils should use.
Changelog π
v2.2.0
- Update some dependencies #180.
- Enable
0value forsetmethod #125 #181. - Fix
timezoneOffsetutil date parsing #183.
v2.1.0
- Adding
setmethods :
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