All Projects → urin → qrono

urin / qrono

Licence: MIT license
🕥 Just right date time library

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects
shell
77523 projects

Projects that are alternatives of or similar to qrono

lit-date
Light-weight, faster datetime formatter for modern browsers.
Stars: ✭ 33 (-70.27%)
Mutual labels:  time, datetime, date
Date Picker
📅 Custom responsive date picker widget for Android, written in Kotlin.
Stars: ✭ 146 (+31.53%)
Mutual labels:  time, datetime, date
Calendar
📅 PHP Date & Time library that solves common problems in object oriented, immutable way.
Stars: ✭ 113 (+1.8%)
Mutual labels:  time, datetime, date
date-extractor
Extract dates from text
Stars: ✭ 58 (-47.75%)
Mutual labels:  time, datetime, date
Dpicker
A framework-agnostic minimal date picker
Stars: ✭ 187 (+68.47%)
Mutual labels:  time, datetime, date
Dateparse
GoLang Parse many date strings without knowing format in advance.
Stars: ✭ 1,365 (+1129.73%)
Mutual labels:  time, datetime, date
Delorean
Delorean: Time Travel Made Easy
Stars: ✭ 1,793 (+1515.32%)
Mutual labels:  time, datetime, date
Iso8601
Ruby parser to work with ISO8601 dateTimes and durations — http://en.wikipedia.org/wiki/ISO_8601
Stars: ✭ 70 (-36.94%)
Mutual labels:  time, datetime, date
Date
A date and time library based on the C++11/14/17 <chrono> header
Stars: ✭ 2,389 (+2052.25%)
Mutual labels:  time, datetime, date
Time
Building a better date/time library for Swift
Stars: ✭ 1,983 (+1686.49%)
Mutual labels:  time, datetime, date
Luatz
Time, Date and Timezone library for lua
Stars: ✭ 92 (-17.12%)
Mutual labels:  time, datetime, date
ptera
Ptera is DateTime library for Deno
Stars: ✭ 62 (-44.14%)
Mutual labels:  time, datetime, date
Graphql Java Datetime
GraphQL ISO Date is a set of RFC 3339 compliant date/time scalar types to be used with graphql-java.
Stars: ✭ 89 (-19.82%)
Mutual labels:  time, datetime, date
Time Stamp
Get a formatted timestamp. Used in gulp, assemble, generate, and many others.
Stars: ✭ 104 (-6.31%)
Mutual labels:  time, datetime, date
Carbon
A simple PHP API extension for DateTime
Stars: ✭ 75 (-32.43%)
Mutual labels:  time, datetime, date
Tail.datetime
A lightweight, translat- and configurable Open Source DateTime Picker, written in pure vanilla JavaScript!
Stars: ✭ 139 (+25.23%)
Mutual labels:  time, datetime, date
Laydate
layDate(日期与时间组件) 是 layui 独立维护的三大组件之一
Stars: ✭ 1,066 (+860.36%)
Mutual labels:  time, datetime, date
When
A natural language date/time parser with pluggable rules
Stars: ✭ 1,113 (+902.7%)
Mutual labels:  time, datetime, date
Gostradamus
Gostradamus: Better DateTimes for Go 🕰️
Stars: ✭ 148 (+33.33%)
Mutual labels:  time, datetime, date
Jiffy
Jiffy is a Flutter (Android, IOS and Web) date time package inspired by momentjs for parsing, manipulating, querying and formatting dates
Stars: ✭ 238 (+114.41%)
Mutual labels:  time, datetime, date

Qrono - 🕥 Just right date time library

MIT License NPM version NPM downloads gzip size

qrono('2021-08-31 12:34').plus({ month: 1 }).isSame(qrono('2021-09-30 12:34'))
qrono('2021-08-31 12:34') < qrono('2021-09-30 12:34')
qrono({ localtime: true }, '2021-08-31 12:34').toString() === '2021-08-31T12:34.000-04:00'

🎨 Design philosophy | 📥 Getting started | 🚀 Quick tour | 🌏 License


Design philosophy 🎨

  • Provides immutable and chain-able functions that are necessary in most cases.
  • Locality-Agnostic.
  • Supports only UTC (as default) and local time of the environment.
    • Other libraries tend to have bigger code base and complicated usage to support multiple time zones and locales.
    • In most cases, it is enough to support only the time zone of the client environment. The Luxon's explanation is right to the point.
  • Enables to handle ambiguous time of daylight saving time strictly.
    • This feature can be achieved thanks to the abandonment of support time zones and locales.
  • Follows ISO 8601.
  • Pure JavaScript without dependencies.
Alternatives
  • Moment.js
    It is a great library that is very widely used (it was the de-facto standard). It went into maintenance mode in 2020.
    It has a fundamental problem that its behavior as a mutable object is prone to bugs, and the later date-time libraries introduced below are all designed to be immutable.
  • Luxon
    An immutable and rich library created by the maintainers of Moment.js. Sophisticated and feature-rich. Good code base to explore.
    By default, it handles time in local time, and cannot strictly handle ambiguous times.
    It is different from other libraries in that the documentation clearly shows how it behaves with ambiguous time.
  • Day.js
    A Moment.js compatible library with a minimum size of 2KB, which has many GitHub stars and is becoming the de-facto standard. The code readability is not high.
    The code base is large due to time zone and locale support (178 source files as of 2021-11-02), but the effective size can be reduced if tree-shaking is available.
    Requires to import plugins each time because most of functions are provided as plugins.
    Planning a major version upgrade in the middle of solving the large number of issues.
  • date-fns
    As the name implies, it provides over 200 pure functions for manipulating JavaScript Date objects, implemented in TypeScript and tree-shaking enabled.
    Since the JavaScript Date object takes the lead, problems such as mutable, month starting at 0, etc. are inherited.
  • The ECMA TC39 Temporal Proposal
    An ECMAScript® API proposal that may become a future standard. The specification is rigorous, spectacular, and inspired by java.time.

Getting started 📥

Browser

<script src="path/to/qrono.min.js"></script>
<!-- from UNPKG -->
<script src="https://unpkg.com/qrono/dist/qrono.min.js"></script>

Node.js

npm install qrono
// as module
import qrono from 'qrono'
// or CommonJS
const { qrono } = require('qrono')

Quick tour 🚀

Construction

// now
qrono()
// from various type of arguments
qrono('2022-12-31') // => 2022-12-31T00:00:00.000Z
qrono(new Date())
// followings are same 2022-12-31T15:23:11.321Z
qrono('2022-12-31 15:23:11.321')
qrono(1672500191321)
qrono(2022, 12, 31, 15, 23, 11, 321)
qrono([2022, 12, 31, 15, 23, 11, 321])
qrono({ year: 2022, month: 12, day: 31, hour: 15, minute: 23, second: 11, millisecond: 321 })

Accessor

const time = qrono(2022, 12, 31, 15, 23, 11, 321)
time.year()        // => 2022
time.month()       // => 12
time.day()         // => 31
time.hour()        // => 15
time.minute()      // => 23
time.second()      // => 11
time.millisecond() // => 321

time.second(0) // => returns new Qrono instance

Time zone

// UTC as default
qrono('2022-12-31 15:23:11.321').toString() // => "2022-12-31T15:23:11.321Z"
// set default to local time
qrono.asLocaltime()
qrono('2022-12-31 15:23:11.321').toString()     // => "2022-12-31T15:23:11.321-04:00"
qrono('2022-12-31 15:23:11.321').asUtc().hour() // => 11 as UTC
qrono('2022-12-31 15:23:11.321').hour()         // => 15 as local time

Conversion

qrono('2000-01-01').numeric() // => 946,684,800,000 milliseconds from UNIX epoch
  === +qrono('2000-01-01')    // => true
const time = qrono('2000-01-02 03:04:05.006')
time.toObject()   // => { year: 2000, month: 1, day: 2, hour: 3, minute: 4, second: 5, millisecond: 6 }
time.toArray()    // => [2000, 1, 2, 3, 4, 5, 6]
time.nativeDate() // => JavaScript native Date instance

Calculation

qrono('2000-01-01 01:00:00.000') - qrono('2000-01-01') // => 3,600,000 milliseconds = 1 hour
qrono('2000-01-01 01:00:00.000') < qrono('2000-01-01') // => false
qrono('2000-01-01').plus(7200000).minus(3600000)       // => 2000-01-01T01:00:00.000Z
qrono('2000-01-01').minus({ hour: 1, minute: 30 })     // => 1999-12-31T22:30:00.000Z

const today = qrono()
const yesterday = today.minus({ day: 1 })
const tommorrow = today.plus({ day: 1 })
today.isBetween(yesterday, tommorrow) // => true

Short-hands

const time = qrono('2000-01-02 03:04:05.006')
time.startOfYear()   // => 2000-01-01T00:00:00.000Z
time.startOfMonth()  // => 2000-01-01T00:00:00.000Z
time.startOfDay()    // => 2000-01-02T00:00:00.000Z
time.startOfHour()   // => 2000-01-02T03:00:00.000Z
time.startOfMinute() // => 2000-01-02T03:04:00.000Z
time.startOfSecond() // => 2000-01-02T03:04:05.000Z
time.dayOfWeek()     // => 7 === qrono.sunday
time.dayOfYear()     // => 2
time.isLeapYear()    // => true
time.daysInMonth()   // => 31
time.daysInYear()    // => 366
// ISO week number. See https://en.wikipedia.org/wiki/ISO_week_date
time.weeksInYear()   // => 52
time.weekOfYear()    // => 52
time.yearOfWeek()    // => 1999
// Daylight saving time stuff that is meaningful in case of local time
const localtime = time.asLocaltime()
localtime.hasDstInYear()
localtime.isInDst()
localtime.isDstTransitionDay()
localtime.minutesInDay()

QronoDate

qrono.date(...) returns a QronoDate instance with only date information.

Methods of QronoDate are almost compatible with those of Qrono.

qrono.date('2000-01-02').toString()       // => "2000-01-02"
qrono('2000-01-02 23:04:05.006').toDate() // => QronoDate instance 2000-01-02
qrono.date('2000-01-02').numeric()        // => 10958 days from UNIX epoch

License 🌏

MIT

Copyright (c) 2021 Urin

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].