timestamp-utils

2.2.0 • Public • Published

timestamp-utils

⏱️ An anwsome and tiny util package for timestamp without dependencies


Bundlephobia XO code style npm version

Navigation 🗺️ :

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-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 :
    • 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 : 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 :
    • 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 : add calls addMilliseconds, addSeconds, addMinutes, addHours, addDays, addMonths and addYears in this order.
That mean, according to addMonths's note, add(t, { days: -1, months: -1 }) and addDays(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 : weekDay must 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 0 value for set method #125 #181.
  • Fix timezoneOffset util date parsing #183.

v2.1.0

v2.0.2

v2.0.1

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)
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

License 🖋

MIT

Package Sidebar

Install

npm i timestamp-utils

Weekly Downloads

192

Version

2.2.0

License

MIT

Unpacked Size

22.2 kB

Total Files

4

Last publish

Collaborators

  • arncet