$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>
Or get typescript sources from repository: https://github.com/nin-jin/pms-jin/tree/master/time
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
- fastest
Speed of iso8601 serialization:
var m = new Date ; console.time('test') ; for( var i = 0 ; i < 100000 ; ++i ) m.toISOString() ; console.timeEnd('test')
Speed of iso8601 parsing:
console.time('test') ; for( var i = 0 ; i < 10000 ; ++i ) new Date( '2015-07-20T07:48:28.338+03:00' ) ; 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')
Speed of iso8601 parsing:
console.time('test') ; for( var i = 0 ; i < 10000 ; ++i ) moment( '2015-07-20T07:48:28.338+03:00' ) ; 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')
Speed of iso8601 parsing:
console.time('test') ; for( var i = 0 ; i < 10000 ; ++i ) $jin.time.moment( '2015-07-20T07:48:28.338+03:00' ) ; 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' )
Serialization
$jin.time.moment().toString( 'YYYY-MM-DD hh:mm (WeekDay)' )
Mnemonics:
- single letter for numbers: M - month number, D - day of month.
- uppercase letters for dates, lowercase for times: M - month number , m - minutes number
- repeated letters for define register count: YYYY - full year, YY - shot year, MM - padded month number
- words for word representation: Month - month name, WeekDay - day of week name
- shortcuts: WD - short day of week, Mon - short month name.
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({
start : '2015-07-19' ,
end : '2015-08-02' ,
})
$jin.time.range({
start : '2015-07-19' ,
duration : 'P14D' ,
})
$jin.time.range({
duration : 'P14D' ,
end : '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' ).end
$jin.time.range( 'P1Y/2016' ).start
$jin.time.range( '2015/2016' ).duration
$jin.time.range( '2015-01/P1M' ).toString()
$jin.time.range( '2015-01/P1M' ).toJSON()
Localization
To add some language support use typescript subclassing:
class moment_class_ru extends $jin.time.moment_class {
static monthLong = [ 'Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь' ]
static monthShort = [ 'Янв', 'Фев', 'Мар', 'Апр', 'Май', 'Июн', 'Июл', 'Авг', 'Сен', 'Окт', 'Ноя', 'Дек' ]
static weekDayLong = [ 'Воскресенье', 'Понедельник', 'Вторник', 'Среда', 'Четверг', 'Пятница', 'Суббота' ]
static weekDayShort = [ 'Вс', 'Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб' ]
}
$jin.time.moment['ru'] = moment_class_ru.make.bind( moment_class_ru )
Then you can use it:
$jin.time.moment['ru']().toString( 'YYYY-MM-DD (WD)' )
To Do
- Parsing by patterns
- Repeating intervals support
- Better localization