npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

datetime-periods

v1.2.0

Published

Generates a valid list of years down to seconds for a date and min and max date

Downloads

33

Readme

datetime-periods

Build Status Codacy Badgecodecov

Get a valid list of years down to seconds for a date and min and max date.

You ever wanted to create a simple date time picker or wanted to show the user available dates or only the days?

It is a zero dependecy lib for browser and node environments.

Donate/Support

If you like my work, feel free to support it. Donations to the project are always welcomed :)

PayPal: PayPal.Me/bengtler

BTC Wallet Address: 3QVyr2tpRLBCw1kBQ59sTDraV6DTswq8Li

ETH Wallet Address: 0x394d44f3b6e3a4f7b4d44991e7654b0cab4af68f

LTC Wallet Address: MFif769WSZ1g7ReAzzDE7TJVqtkFpmoTyT

XRP Wallet Address: rXieaAC3nevTKgVu2SYoShjTCS2Tfczqx?dt=159046833

Installation

  • npm install datetime-periods
  • yarn add datetime-periods

Usage

Script tag

If you want to use datetime-periods via script tag, you should use the datetime-periods.umd.js. After the script is loaded you can access the methods through the global datetimePeriods.

<html>
  <head>
    <script src="../lib/datetime-periods.umd.js"></script>
    <script>
      // functions are stored globally on the window object
      console.log('window.datetimePeriods:', window.datetimePeriods)
    </script>
  </head>
  <body>
  </body>
</html>

ESModules or Typescript

Just import the methods you need from the package.

import { getDateTimeObject } from 'datetime-periods'
import * as dateTimePeriods from 'datetime-periods'

Node and require

Just require the methods you need from the package.

const getDateTimeObject = require('datetime-periods').getDateTimeObject
const dateTimePeriods = require('datetime-periods')

Example

Some simple and easy to use examples can be found in the example folder in the repo.

There is also a plain JavaScript example page.

Or use RunKit to try it out!

Methods

getDateTimeObject(date: Date)

Transforms a date in an object respresentation.

Result value:

// DateTimeObject interface
{
  year: number
  month: number // (1-12)
  day: number
  hour: number
  minute: number
  second: number
  tzOffset: number
}

Example:

import { getDateTimeObject } from 'datetime-periods'

const dateTimeObject = getDateTimeObject(new Date())

console.log(dateTimeObject)

Output:

{
  "day": 14,
  "hour": 13,​
  "minute": 15,
  "month": 3​,
  "second": 15,
  "tzOffset": -60​,
  "year": 2019
}

getDaysInMonth(year: number, month: number)

Returns the day count of a month and year. The month is based on numbers from 1 (january) to 12 (december).

Example:

import { getDaysInMonth } from 'datetime-periods'

const daysInFebruary2019 = getDaysInMonth(2, 2019)

console.log(daysInFebruary2019)

Output:

28

getDateTimePeriods(value: Date, min: Date (optional), max: Date (optional), needed: NeededPeriods (optional))

This method contains the main functionality of the lib. The other methods are only internally used, but maybe someone finds them useful :).

It generates a data structure of valid years, months, days, hours, minutes and seconds for a given date and an optional min and max date. In addition to that it returns all the calculation dates as object representation.

If only some period values are interesting, it is possible to only retrieve, years, months, ... . Therefore use the needed parameter and pass an object of the values you do not need. This periods keys will be an empty array.

getDateTimePeriods(new Date(), undefined, undefined, {
  days: false
  years: false
})

Result value

{
  value: DateTimeObject // validated value as object
  originalValue: DateTimeObject // original value as object
  originalValueChanged: boolean // indicator if original has changed
  max: DateTimeObject // max date as object
  min: DateTimeObject // min date as object
  periods: {
    days: number[] // available days
    hours: number[] // available hours
    minutes: number[] // available minutes
    months: number[] // available months
    seconds: number[] // available seconds
    years: number[] // available years
  }
}

Example:

import { getDateTimePeriods } from 'datetime-periods'

const value = new Date()

const min = new Date()
min.setFullYear(value.getFullYear() - 5)

const max = new Date()
max.setFullYear(value.getFullYear() + 5)

const result = getDateTimePeriods(value, min, max)

console.log(result)

Output:

{
  "value": {
    "year": 2019,
    "month": 3,
    "day": 14,
    "hour": 14,
    "minute": 40,
    "second": 21,
    "tzOffset": -60
  },
  "originalValue": {
    "year": 2019,
    "month": 3,
    "day": 14,
    "hour": 14,
    "minute": 40,
    "second": 21,
    "tzOffset": -60
  },
  "originalValueChanged": false,
  "max": {
    "year": 2024,
    "month": 1,
    "day": 1,
    "hour": 1,
    "minute": 0,
    "second": 2,
    "tzOffset": -60
  },
  "min": {
    "year": 2014,
    "month": 3,
    "day": 14,
    "hour": 14,
    "minute": 40,
    "second": 21,
    "tzOffset": -60
  },
  "periods": {
    "days": [
      1,
      2,
      3,
      ...
      28,
      29,
      30,
      31
    ],
    "hours": [
      0,
      1,
      2,
      ...
      21,
      22,
      23
    ],
    "minutes": [
      0,
      1,
      2,
      3,
      ...
      56,
      57,
      58,
      59
    ],
    "months": [
      1,
      2,
      3,
      ...
      10,
      11,
      12
    ],
    "seconds": [
      0,
      1,
      2,
      ...
      57,
      58,
      59
    ],
    "years": [
      2014,
      2015,
      2016,
      ...
      2022,
      2023,
      2024
    ]
  }
}

Default min and max values

  • min: value - 100 years
  • max: value + 100 years

Special cases

Leading to a console.warn!

  • min > max: min and max are fall back to the default
  • value < min: value is set to min, sets originalValueChanged to true
  • value > max: value is set to max, sets originalValueChanged to true