$jin.time
Small, simple, powerfull, and fast TypeScript/JavaScript library for proper date/time/duration/range arithmetic.
Installation
Direct
<script src="http://nin-jin.github.io/time/time2.js"></script>
<script>
alert( $jin.time.moment() )
</script>
NPM
npm install jin-time
var time = require( 'jin-time' )
console.log( time.moment().toString() )
Comparison
Native Date
- instance is timestamp representation (forever have time and timezone)
- instance is mutable object
- inconsistent, poor api
- zero lib size, but very large client code
Speed of iso8601 serialization:
var m = new Date ; console.time('test') ; for( var i = 0 ; i < 100000 ; ++i ) m.toISOString() ; console.timeEnd('test')
MomentJS
- instance is wrapper around native Date instance
- instance is timestamp representation (forever have time and timezone)
- instance is mutable object
- large lib size (100kb core + plugins)
- slow
- too more specific features, but some basics supports only by plugins
Speed of iso8601 serialization:
var m = moment() ; console.time('test') ; for( var i = 0 ; i < 100000 ; ++i ) t.toISOString() ; console.timeEnd('test')
$jin.time
- all time components stores separately
- instance is immmutable object
- cool consistent core, but some needed features not implemented yet
- small lib size (35kb)
- fast
Speed of iso8601 serialization:
var m = $jin.time.moment() ; console.time('test') ; for( var i = 0 ; i < 100000 ; ++i ) m.toString() ; console.timeEnd('test')
API
Moments
Creating
$jin.time.moment()
$jin.time.moment( 1437316165000 )
$jin.time.moment( new Date )
$jin.time.moment( '2015-07-19T17:27:58.065+03:00' )
$jin.time.moment( '2015-07-19T17:27:58.065' )
$jin.time.moment( '2015-07-19' )
$jin.time.moment({
year : 2015 ,
month : 6 ,
day : 18 ,
hour : 17 ,
minute : 27 ,
second : 58.65 ,
offset : {
hour : 3 ,
minute : 0 ,
} ,
})
$jin.time.moment([ 2015 , 6 , 18 , 17 , 27 , 58.65 , [ 3 , 0 ]])
Getters
var moment = $jin.time.moment()
moment.year
moment.month
moment.day
moment.hour
moment.minute
moment.second
moment.offset
$jin.time.moment().weekDay
$jin.time.moment().valueOf()
$jin.time.moment().native
$jin.time.moment().toString()
$jin.time.moment().toJSON()
Arithmetic
$jin.time.moment( '2015-07-35' ).normal
$jin.time.moment( '2015-07-19' ).merge({ month : 7 , day : 4 })
$jin.time.moment( '2015-07-19' ).shift( 'P16D' )
$jin.time.moment( '2015-07-19T19:24+03:00' ).toOffset( 'Z' )
Durations
Creating
$jin.time.duration()
$jin.time.duration( 60000 )
$jin.time.duration( 'P1Y2M3DT4H5M6.7S' )
$jin.time.duration( 'PT' )
$jin.time.duration({
year : 1 ,
month : 2 ,
day : 3 ,
hour : 4 ,
minute : 5 ,
second : 6.7 ,
})
$jin.time.duration([ 1 , 2 , 3 , 4 , 5 , 6.7 ])
Getters
var dur = $jin.time.duration()
durdur.year
dur.month
dur.day
dur.hour
dur.minute
dur.second
$jin.time.duration( 'PT1M' ).valueOf()
$jin.time.duration( 'PT1M0S' ).toString()
$jin.time.duration( 'PT1M0S' ).toJSON()
Arithmetic
$jin.time.duration( 'PT1h' ).summ( 'PT1h1m' )
$jin.time.duration( 'PT1h' ).sub( 'PT1h1m' )
Ranges
Creating
$jin.time.range( '2015-07-19/2015-08-02' )
$jin.time.range( '2015-07-19/P14D' )
$jin.time.range( 'P14D/2015-08-02' )
$jin.time.range({
from : '2015-07-19' ,
to : '2015-08-02' ,
})
$jin.time.range({
from : '2015-07-19' ,
duration : 'P14D' ,
})
$jin.time.range({
duration : 'P14D' ,
to : '2015-08-02' ,
})
$jin.time.range([ '2015-07-19' , '2015-08-02' ])
$jin.time.range([ '2015-07-19' , null , 'P14D' ])
$jin.time.range([ null , '2015-08-02' , 'P14D' ])
Getters
$jin.time.range( '2015/P1Y' ).to
$jin.time.range( 'P1Y/2016' ).from
$jin.time.range( '2015/2016' ).duration
$jin.time.range( '2015-01/P1M' ).toString()
$jin.time.range( '2015-01/P1M' ).toJSON()
To Do
- Parsing by patterns
- Repeating intervals support
- Better localization