我正在寻找coffescript并且找不到任何好的库来处理像Rails这样的日期函数
所以我写了一些可以帮助你完成工作的东西。
如果你找到更好的方法,请评论:)
用法:
(new Date()).endOfMonth()
#=> Fri Feb 01 2013 00:00:00 GMT+0100 (CET)
(new Date()).subMonths(2).beginningOfMonth()
#=> Sat Dec 01 2012 00:00:00 GMT+0100 (CET)
库:
Date::yesterday = () ->
new Date(1900+@getYear(), @getMonth(), @getDate()-1)
Date::beginningOfYear = () ->
new Date(1900+@getYear(), 1, 1)
Date::endOfYear = () ->
new Date(1900+@getYear()+1, 0, 0)
Date::beginningOfMonth = () ->
new Date(1900+@getYear(), @getMonth(), 1)
Date::endOfMonth = () ->
new Date(1900+@getYear(), @getMonth()+1, 0)
Date::beginningOfWeek = () ->
new Date(1900+@getYear(), @getMonth(), @getDate() - @getDay() + 1)
Date::endOfWeek = () ->
new Date(1900+@getYear(), @getMonth(), @getDate() + (7-@getDay()))
Date::addYears = (count) ->
new Date(1900+@getYear() + count, @getMonth(), @getDate())
Date::addMonths = (count) ->
new Date(1900+@getYear(), @getMonth() + count, @getDate())
Date::addDays = (count) ->
new Date(1900+@getYear(), @getMonth(), @getDate() + count)
Date::subYears = (count) ->
new Date(1900+@getYear() - count, @getMonth(), @getDate())
Date::subMonths = (count) ->
new Date(1900+@getYear(), @getMonth() - count, @getDate())
Date::subDays = (count) ->
new Date(1900+@getYear(), @getMonth(), @getDate() - count)