Coffeescript:Rails日期功能的方式 - 开头和&年末/月/周

时间:2013-02-12 20:59:12

标签: date coffeescript

我正在寻找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)  

1 个答案:

答案 0 :(得分:1)

在处理时间时,我不能真正推荐moment.js