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

zeit.js

v1.0.5

Published

JavaScript utility to carry out date time calculations

Downloads

2

Readme

Zeit.js

Zeit.js (Zeit stands for time in German language) is a library for datetime calculation. It's not meant for a replacement for moment.js. Rather it's a toolkit that you'll appreciate when doing heavy datetime calculation, such as creating a custom calendar, working with timeseries data, or calculating range of dates.

Initializing a new object

// Innitialize without values, it is the same as `new Date()`
const zeit = new Zeit()

// Initialize with the date object
const zeit = new Zeit(new Date(2016, 0, 1)) 

// Initialize with year and month
const zeit = new Zeit(2016, 0)

// Initialize with year, month and date
const zeit = new Zeit(2016, 0, 31)

// Initialize with timestamp
const zeit = new Zeit(1480562888484)

// Setter, you can optionally set the date later
zeit.value = new Date()

// Getter, this is how you access back the original date object value
console.log(zeit.value) // Thu Dec 01 2016 11:16:27 GMT+0800 (MYT)
console.log(zeit.now) // 1480562166515, Timestamp 

Comparison with the Date Object

// This is how it will look like when you use the JavaScript date object
const date = new Date(2016, 0, 1)
const year = date.getFullYear() // 2016
const month = date.getMonth() // 0
// The reason I pick zeit is to avoid this conflict in naming :)
const date1 = date.getDate() // 1

// With Zeit.js
const zeit = new Zeit(2016, 0, 1)
const year = zeit.year // 2016
const month = zeit.month // 0
const date = zeit.date // 1

Date

const zeit = new Zeit(2016, 0, 1)
console.log(zeit.date) // prints 1 

Day

A list of getters for day

const zeit = new Zeit(2016, 0, 1)
console.log('zeit.day', zeit.day) // 5
console.log('zeit.dayStart', zeit.dayStart) // Fri Jan 01 2016 00:00:00 GMT+0800 (MYT)
console.log('zeit.dayEnd', zeit.dayEnd) // Fri Jan 01 2016 23:59:59 GMT+0800 (MYT)
console.log('zeit.dayStringShort', zeit.dayStringShort) // Fri
console.log('zeit.dayStringLong', zeit.dayStringLong) // Friday
console.log('zeit.daysInAMonth', zeit.daysInAMonth) // 31
console.log('zeit.daysInAMonthOffset', zeit.daysInAMonthOffset) // 35

Week

console.log('zeit.weekStart', zeit.weekStart) // Mon Nov 14 2016 00:00:00 GMT+0800 (MYT)
console.log('zeit.weekEnd', zeit.weekEnd) // Sun Nov 20 2016 23:59:59 GMT+0800 (MYT)
console.log('zeit.weekArray', zeit.weekArray)
console.log('zeit.weekOfTheYearArray', zeit.weekOfTheYearArray)
console.log('zeit.weekOfTheYear', zeit.weekOfTheYear) // 46

Month

const zeit = new Zeit (2016, 0, 1)

console.log('zeit.month', zeit.month) // 0
console.log('zeit.monthStringShort', zeit.monthStringShort) // Jan
console.log('zeit.monthStringLong', zeit.monthStringLong) // January
console.log('zeit.monthStart', zeit.monthStart) // 2015-12-31T16:00:00.000Z
console.log('zeit.monthEnd', zeit.monthEnd) // 2016-01-31T15:59:59.999Z
console.log('zeit.monthStartOffset', zeit.monthStartOffset) // 2015-12-27T16:00:00.000Z
console.log('zeit.monthEndOffset', zeit.monthEndOffset) // 2016-01-31T15:59:59.999Z
console.log('zeit.monthMid', zeit.monthMid) // 2016-01-14T16:00:00.000Z
console.log('zeit.nextMonth', zeit.nextMonth) // 2016-01-31T16:00:00.000Z
console.log('zeit.prevMonth', zeit.prevMonth) // 2015-11-30T16:00:00.000Z
console.log('zeit.calendar', zeit.calendar)
console.log('zeit.monthArray', zeit.monthArray)
console.log('zeit.monthOffsetArray', zeit.monthOffsetArray)
console.log('zeit.monthCalendar', zeit.monthCalendar)

Year

const zeit = new Zeit (2016, 0, 1)

console.log('zeit.year', zeit.year) // 2016
console.log('zeit.yearStart', zeit.yearStart) // 2015-12-31T16:00:00.000Z
console.log('zeit.yearEnd', zeit.yearEnd) // 2016-12-31T15:59:59.999Z
console.log('zeit.yearStartOffset', zeit.yearStartOffset) //  2015-12-27T16:00:00.000Z
console.log('zeit.yearEndOffset', zeit.yearEndOffset) // 2017-01-01T15:59:59.999Z

Booleans

const zeit = new Zeit (2016, 0, 1)

console.log('zeit.isToday', zeit.isToday) // false
console.log('zeit.isThisWeek', zeit.isThisWeek) // false
console.log('zeit.isThisMonth', zeit.isThisMonth) // false
console.log('zeit.isThisYear', zeit.isThisYear) // true
console.log('zeit.isLeapYear', zeit.isLeapYear) // true

Counts

console.log('zeit.daysInAYear', zeit.daysInAYear) // 366
console.log('zeit.daysInAMonth', zeit.daysInTheMonth) // 30
console.log('zeit.daysInAMonthOffset', zeit.daysInAMonthOffset) // 30
console.log('zeit.dayOfTheYear', zeit.dayOfTheYear) // 320

console.log('zeit.weeksInAYear', zeit.weeksInAYear) // 52
console.log('zeit.weekOfTheYear', zeit.weekOfTheYear) 
console.log('zeit.weekOfTheMonth', zeit.weekOfTheMonth) // 3

console.log('zeit.progress', zeit.progress) // 86.88524590163934

Use case

Creating a calendar

// This will return an array of dates for this month
const calendar = new Zeit(2016, 0, 1).calendar()

Calculating range of time

When querying a database for a range of time

// year
function queryBy (range) {
    const zeit = new Zeit()
    let start = null
    let end = null
    switch (range) {
        case 'year': start = zeit.yearStart; end = zeit.yearEnd; break;
        case 'month': start = zeit.monthStart; end = zeit.monthEnd; break;
        case 'week': start = zeit.weekStart; end = zeit.weekEnd; break;
        case 'day': start = zeit.dayStart; end = zeit.dayEnd; break;
        default: start = zeit.dayStart; end = zeit.dayEnd;
    }
    return db.collection.find({ 
        created_at: {
            '$gte': start,
            '$lte': end
        } 
    })
}
queryBy('date').then((results) => {
    // Do something with date results
})