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
const zeit = new Zeit()
const zeit = new Zeit(new Date(2016, 0, 1))
const zeit = new Zeit(2016, 0)
const zeit = new Zeit(2016, 0, 31)
const zeit = new Zeit(1480562888484)
zeit.value = new Date()
console.log(zeit.value)
console.log(zeit.now)
Comparison with the Date Object
const date = new Date(2016, 0, 1)
const year = date.getFullYear()
const month = date.getMonth()
const date1 = date.getDate()
const zeit = new Zeit(2016, 0, 1)
const year = zeit.year
const month = zeit.month
const date = zeit.date
Date
const zeit = new Zeit(2016, 0, 1)
console.log(zeit.date)
Day
A list of getters for day
const zeit = new Zeit(2016, 0, 1)
console.log('zeit.day', zeit.day)
console.log('zeit.dayStart', zeit.dayStart)
console.log('zeit.dayEnd', zeit.dayEnd)
console.log('zeit.dayStringShort', zeit.dayStringShort)
console.log('zeit.dayStringLong', zeit.dayStringLong)
console.log('zeit.daysInAMonth', zeit.daysInAMonth)
console.log('zeit.daysInAMonthOffset', zeit.daysInAMonthOffset)
Week
console.log('zeit.weekStart', zeit.weekStart)
console.log('zeit.weekEnd', zeit.weekEnd)
console.log('zeit.weekArray', zeit.weekArray)
console.log('zeit.weekOfTheYearArray', zeit.weekOfTheYearArray)
console.log('zeit.weekOfTheYear', zeit.weekOfTheYear)
Month
const zeit = new Zeit (2016, 0, 1)
console.log('zeit.month', zeit.month)
console.log('zeit.monthStringShort', zeit.monthStringShort)
console.log('zeit.monthStringLong', zeit.monthStringLong)
console.log('zeit.monthStart', zeit.monthStart)
console.log('zeit.monthEnd', zeit.monthEnd)
console.log('zeit.monthStartOffset', zeit.monthStartOffset)
console.log('zeit.monthEndOffset', zeit.monthEndOffset)
console.log('zeit.monthMid', zeit.monthMid)
console.log('zeit.nextMonth', zeit.nextMonth)
console.log('zeit.prevMonth', zeit.prevMonth)
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)
console.log('zeit.yearStart', zeit.yearStart)
console.log('zeit.yearEnd', zeit.yearEnd)
console.log('zeit.yearStartOffset', zeit.yearStartOffset)
console.log('zeit.yearEndOffset', zeit.yearEndOffset)
Booleans
const zeit = new Zeit (2016, 0, 1)
console.log('zeit.isToday', zeit.isToday)
console.log('zeit.isThisWeek', zeit.isThisWeek)
console.log('zeit.isThisMonth', zeit.isThisMonth)
console.log('zeit.isThisYear', zeit.isThisYear)
console.log('zeit.isLeapYear', zeit.isLeapYear)
Counts
console.log('zeit.daysInAYear', zeit.daysInAYear)
console.log('zeit.daysInAMonth', zeit.daysInTheMonth)
console.log('zeit.daysInAMonthOffset', zeit.daysInAMonthOffset)
console.log('zeit.dayOfTheYear', zeit.dayOfTheYear)
console.log('zeit.weeksInAYear', zeit.weeksInAYear)
console.log('zeit.weekOfTheYear', zeit.weekOfTheYear)
console.log('zeit.weekOfTheMonth', zeit.weekOfTheMonth)
console.log('zeit.progress', zeit.progress)
Use case
Creating a calendar
const calendar = new Zeit(2016, 0, 1).calendar()
Calculating range of time
When querying a database for a range of time
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) => {
})