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

miment

v0.0.9

Published

A fast, light weight, simple and non-dependencies time library for JavaScript (mini moment)

Downloads

1,099

Readme

Miment

Miment is an aero-weigh time library (~1KB minified) but get things done efficiently.

Build Status Badge Size Version LICENSE

Why

Moment is an extraordinary time library that almost covers everything you need, with a massive (200KB+) size. Suppose 90% of its daily usage can be fulfilled within 1% codebase, this tradeoff can be worthwhile. So there comes miment as "mini-moment".

Install

For NPM environment:

npm i miment

Import into codebase and go:

import miment from 'miment'
miment().format('YYYY/MM/DD hh-mm-ss SSS') // 2018/04/09 23-49-36 568

For browser usage, simply include ./dist/miment-min.js:

<script src="https://unpkg.com/miment/dist/miment-min.js"></script>
<script>
  miment().format('YYYY/MM/DD hh-mm-ss SSS') // 2018/04/09 23-49-36 568
</script>

Walkthrough

Format Time

For most daily usage, format is what you need:

miment().format() // 2018-04-09 23:49:36
miment().format('YYYY/MM/DD hh-mm-ss SSS') // 2018/04/09 23-49-36 568
miment().format('YYYY年MM月DD日 星期WW') // 2018年04月09日 星期一
miment().format('YYYY年MM月DD日 星期ww') // 2018年04月09日 星期1 *周日对应星期0*

What if you only what to get a date field?

miment().format('YYYY') // '2018'
miment().format('MM') // '04'
miment().format('DD') // '09'
// ...

For JSON format, miment().json() yields:

{
  "year": 2018,
  "month": 4,
  "date": 11,
  "hour": 8,
  "minute": 57,
  "second": 41,
  "day": 3,
  "millisecond": 87
}

For other output formats, you can have miment().stamp() for timestamp, and miment().daysInMonth() for days count in corresponding month.

Calculate Time

What's the date 10 days after? add is what you need:

miment().add(1,'DD') // next day
miment().add(1, 'YYYY').add(2, 'MM').add(-3, 'DD') // chaining
miment().add(-1,'ww') // date last week
miment().add(500,SSS) // add 500ms

Remember not to chain add() after format(), since format() returns string instead of miment instance:

miment().add(1, 'DD').format().add(1, 'DD') // Error!

How to count distance between 2 dates? You have distance:

miment().distance('2018-04-11 00:00:00')
// Thu Jan 08 1970 21:04:50 GMT+0800 (CST)

By default the instance returned by distance is resolved as unix timestamp relative to 1970 "epoch". In this case, use second argument for format API on formatting:

miment().distance(1523408529932).format('YYYY/MM/DD')
// wrong: '1970/01/08'

miment().distance('2018-04-11 00:00:00').format('YYYY/MM/DD', true)
// correct: '0/00/07'

For other common calculation, see reference.

Reference

Factory

miment

miment(string?|Date?) => Miment

For miment instance methods, there are basically 3 kinds of methods available:

  • Chainable methods returning Miment instance, e.g., add and distance.
  • Unchainable methods returning computed values, e.g., format and json.
  • Methods inherited from native Date model.

Chainable

add

add(amount: number, unit: string) => Miment

Get Miment object with offset date added.

miment().add(1, 'DD')
miment().add(1, 'YYYY').add(2, 'MM').add(-3, 'DD')
miment().add(-1, 'ww')
miment().add(500,SSS)

distance

distance(dt: Miment|Date|string, dt2: Miment?|Date?|string?) => Miment

Get Miment object measuring distance between two dates. If only one argument is provided, it returns distance between that time and current time.

To get the formatted result, please format it with the second true flag.

miment().distance('2018-04-10 00:00:00')
// Mon Dec 29 1969 22:11:51 GMT+0800 (CST)

miment().distance(1523408529932)
// Wed Dec 31 1969 07:13:47 GMT+0800 (CST)

miment().distance('2018-04-10 00:00:00', new Date())
// Mon Dec 29 1969 22:11:13 GMT+0800 (CST)

miment().distance('2018-04-10 00:00:00', '2018-04-11 00:00:00')
// Mon Dec 29 1969 22:10:46 GMT+0800 (CST)

firstDayOfWeek

firstDayOfWeek() => Miment

Get Miment object representing first day (Sunday) of the week. For other weekday, simply chaining it with add.

miment().firstDayOfWeek()
// Sun Apr 08 2018 11:27:55 GMT+0800 (CST)

miment().firstDayOfWeek().format()
// 2018-04-08 11:27:55

firstDay

firstDay() => Miment

Get Miment object representing first day of the month.

miment().firstDay()
// Sun Apr 01 2018 00:00:00 GMT+0800 (CST)

miment().firstDay().format()
// 2018-04-01 00:00:00

lastDay

firstDay() => Miment

Get Miment object representing last day of the month.

miment().lastDay()           // Mon Apr 30 2018 00:00:00 GMT+0800 (CST)
miment().lastDay().format()  // 2018-04-30 00:00:00

Computed

format

format(formatter: string?, distance: boolean) => string

Format Miment into string. Support combination of following formats: "YYYY MM DD hh mm ss SSS ww WW". Pass distance flag as true if you are formatting results from distance().

json

json() => Object

Get JSON object for date fields.

{
  "year": 2018,
  "month": 4,
  "date": 11,
  "hour": 8,
  "minute": 57,
  "second": 41,
  "day": 3,
  "millisecond": 87
}

stamp

stamp() => number

Get timestamp of Miment object.

Date API

All Date instance APIs are inherited. For now it's not recommended to use it since they're unchainable.

License

MIT


Please star this repo if you like, and feel free to submit issues, thanks!