An itty library to handle common time-related things for your API needs.

Overview

itty-time

Version Bundle Size Build Status Coverage Status NPM Weekly Downloads Open Issues

Discord GitHub Repo stars Twitter

Tiny (~500 bytes) time math library for making date handling and TTLs within your APIs beautiful.

Features

  • Tiny @ ~500 bytes gzipped total, and tree-shakeable even further.
  • Fully typed/TypeScript support
  • Use plain text strings to describe time, not seconds/milliseconds
  • Get TTLs
  • Get future dates
  • Divide durations

WARNING - pre-release

This API, specifically around the naming of exposed functions may likely change in the next few days. In the meantime, use/play at your own risk. API changes will be broadcasted here and on Discord.

CHANGELOG - pre-release

  • v0.1.x to v0.2.x - renamed: getTTL-->getSeconds, getDatePlus-->datePlus, added optional "from" param to datePlus.
    // old
    getTTL('1 hour')
    // new
    getSeconds('1 hour')
    
    // old
    getDatePlus('1 hour')
    // new
    datePlus('1 hour')
    
    // added functionality
    datePlus('1 hour', optionalDate?: Date)

Example

import { getSeconds, divide, datePlus } from 'itty-time' // under 600 bytes

// Easily get TTL in seconds
getSeconds('3 hours') // 10800

// Complicated bits?  No problem.  (Oxford comma optional)
getSeconds('1 day, 4 hours, and 36 minutes') // 102960

// Need an expiration date?
datePlus('5 seconds') // 2022-10-22T23:10:11.824Z
datePlus('1 minutes') // 2022-10-22T23:11:06.824Z
datePlus('2 months') // 2022-12-23T00:11:58.534Z
datePlus('4 years') // 2026-10-22T23:11:58.534Z

// Want to find out how many X are in Y?
divide('1 week').by('days') // 7
divide('2 minutes').by('seconds') // 120
divide('3 days').by('hours') // 72
divide('1 day').by('3 hours') // 8
divide('1 week').by('seconds') // 604800
divide('24 hours').by('minutes') // 1440
divide('3 days').by('hours') // 72
divide('1 day, 30 minutes').by('hours') // 24.5

DISCLAIMER: This is a CONVENIENCE library for making short, readable code.

If you need absolute max performance for iterative work in a single thread, do the operations manually. That's always faster.

Otherwise, you'll probably never feel the difference, but your code sure will! <3

API

getSeconds(duration: string): number

Returns the number of seconds (as you typically need within a TTL) in the duration string.

getSeconds('3 hours') // 10800
getSeconds('1 day, 4 hours, and 36 minutes') // 102960

datePlus(duration: string, from?: Date): Date

Adds a duration to a date (default is Date.now()), returning the future date.

datePlus('5 seconds') // 2022-10-22T23:10:11.824Z
datePlus('1 minutes') // 2022-10-22T23:11:06.824Z
datePlus('2 months') // 2022-12-23T00:11:58.534Z
datePlus('4 years') // 2026-10-22T23:11:58.534Z

// or from another date
datePlus('1 minutes', datePlus('1 year')) // 2023-10-22T23:11:06.824Z

divide(duration1: string).by(duration2: string): number

Divides one duration by another, in a nice, readable manner.

divide('1 week').by('days') // 7
divide('2 minutes').by('seconds') // 120
divide('3 days').by('hours') // 72
divide('1 day').by('3 hours') // 8
divide('1 week').by('seconds') // 604800
divide('24 hours').by('minutes') // 1440
divide('3 days').by('hours') // 72
divide('1 day, 30 minutes').by('hours') // 24.5
You might also like...

A quick and powerful plugin for your pull-to-refresh needs in your webapp.

A quick and powerful plugin for your pull-to-refresh needs in your webapp.

PulltoRefresh.js • Demos A small, but powerful Javascript library crafted to power your webapp's pull to refresh feature. No markup needed, highly cus

Jan 6, 2023

A small javascript DOM manipulation library based on Jquery's syntax. Acts as a small utility library with the most common functions.

Quantdom JS Quantdom is a very small (about 600 bytes when ran through terser & gzipped) dom danipulation library that uuses a Jquery like syntax and

Aug 16, 2022

Improve the security of your API by detecting common vulnerabilities as defined by OWASP and enforced with Spectral.

Spectral OWASP API Security Scan an OpenAPI document to detect security issues. As OpenAPI is only describing the surface level of the API it cannot s

Dec 8, 2022

375 DSA Tracker helps you build your confidence in solving any coding related question and helps you prepare for your placements. It is your personal web-based progress tracker based on 375 DSA Sheet by Aman Dhattarwal & Shradha Didi

375 DSA Tracker helps you build your confidence in solving any coding related question and helps you prepare for your placements. It is your personal web-based progress tracker based on 375 DSA Sheet by Aman Dhattarwal & Shradha Didi

375-DSA Tracker 👨‍💻 Me and my friend Abhilash Jena made a 375 DSA Tracker website based on 375 DSA Sheet by Aman Dhattarwal & Shradha Didi which hel

Nov 11, 2022

Some process handle JavaScript function parameter.

Function parameter handle or paremeter error control Example 1: Just checking if all arguments were passed / defined. const required = (name) = {

Mar 14, 2022

A set of useful helper methods for writing functions to handle Cloudflare Pub/Sub messages (https://developers.cloudflare.com/pub-sub/)

pubsub A set of useful helper methods for writing functions to handle Cloudflare Pub/Sub messages. This includes: A isValidBrokerRequest helper for au

Dec 4, 2022

A Discord.JS Command Handler to handle commands eaiser

TABLE OF CONTENTS Installation Setup Changing Default Prefix Creating a Command Usage of minArgs and maxArgs Syntax Errors Global Syntax Errors Per Co

Jun 8, 2022

Handle errors like it's 2022 🔮

Handle errors like it's 2022 🔮 Error handling framework that is minimalist yet featureful. Features Minimalist API Custom error types Wrap any error'

Jan 7, 2023

This is just a script I put together to check and notify me via email (MailGun) when there's an earlier date before my initial appointment date. It doesn't handle rescheduling.

This is just a script I put together to check and notify me via email (MailGun) when there's an earlier date before my initial appointment date. It doesn't handle rescheduling.

US-visa-appointment-notifier This is just a script I put together to check and notify me via email (MailGun) when there's an earlier date before my in

Jan 4, 2023
Comments
  • getSeconds sometimes returns different values

    getSeconds sometimes returns different values

    I noticed some weird behavior when trying out getSeconds in an Advent of Vue challenge (that I did in Svelte) and had to replace it sadly.

    Sometimes getSeconds('1 day') and the others return weird values.

    Screenshot 2022-12-04 at 00 53 00

    Here is a minimal repro that showcases it with a flickering number and in the console when using chrome 107.0.5304.121: https://svelte.dev/repl/31907b32e50042d8b8f068148de739a3?version=3.53.1

    EDIT: same on firefox 107.0 (lol they share version numbers atm?) and safari 16.1

    opened by SarcevicAntonio 1
Owner
Kevin R. Whitley
I write things to make your code shorter and more readable. Recently focused on making serverless API development a joyful experience! :)
Kevin R. Whitley
Dead-simple CORS handling for any itty-router API (test with Cloudflare Workers, but works anywhere)!

Simple CORS-handling for any itty-router API. Designed on Cloudflare Workers, but works anywhere. Features Tiny. Currently ~600 bytes, with zero-depen

Kevin R. Whitley 6 Dec 16, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Automated testing for single-page applications (SPAs). Small, portable, and easy to use. Click on things, fill in values, await for things exist, etc.

SPA Check Automated testing for single-page applications (SPAs). Small, portable, and easy to use. Click on things, fill in values, await for things e

Cory Leigh Rahman 5 Dec 23, 2022
Contains html file showcasing Earthquake related data generated in the form of VR model, ArcGIS API with real-time earthquake feed and video of simulation of earthquake generated in blender

Module-EADGI-Project-All about Earthquakes Introduction Contains html file showcasing Earthquake related data generated in the form of VR model, ArcGI

Abhishek Rawat 2 Jun 9, 2022
Helper package to handle requests to a jschan api instance.

jschan-api-sdk Helper package to handle requests to a jschan api instance. How to use npm install ussaohelcim/jschan-api-sdk const { jschan } = requir

Michell 3 Jun 30, 2022
Small TS library to type and safely handle `serde` JSON serializations of Rust enums.

rustie-ts TypeScript library with helper types and functions to type-safely handle Rust's serde JSON serialization of Enums. It can also be used stand

Kelvin Steiner Santos 4 Jul 17, 2022
🍣 CLI to handle Private Git Submodules in your Vercel Project

vercel-submodules: The CLI to handle Private Git Submodules in your Vercel Project This project is community-driven and not affiliated with Vercel in

Juno 37 Jan 1, 2023
Flight is a universal package manager for your needs, no matter what language you may want to write your code in.

Flight Swift, reliable, multi-language package manager. ⚡ Installation We don't have an official release of Flight yet, however, if you would like to

null 26 Dec 25, 2022