File tree Expand file tree Collapse file tree 3 files changed +16
-2
lines changed Expand file tree Collapse file tree 3 files changed +16
-2
lines changed Original file line number Diff line number Diff line change 335335
336336function Calendar :forward ()
337337 self :_ensure_day ()
338- self .date = self .date :start_of ( ' month ' ):add ({ month = vim .v .count1 })
338+ self .date = self .date :set ({ day = 1 } ):add ({ month = vim .v .count1 })
339339 self :render ()
340340 vim .fn .cursor (2 , 1 )
341341 vim .fn .search (' 01' )
344344
345345function Calendar :backward ()
346346 self :_ensure_day ()
347- self .date = self .date :start_of ( ' month ' ):subtract ({ month = vim .v .count1 }):end_of ( ' month ' )
347+ self .date = self .date :set ({ day = 1 } ):subtract ({ month = vim .v .count1 }):last_day_of_month ( )
348348 self :render ()
349349 vim .fn .cursor (8 , 0 )
350350 vim .fn .search ([[ \d\d]] , ' b' )
Original file line number Diff line number Diff line change @@ -498,6 +498,10 @@ function Date:end_of(span)
498498 return self
499499end
500500
501+ function Date :last_day_of_month ()
502+ return self :set ({ day = Date ._days_of_month (os_date (self .timestamp )) })
503+ end
504+
501505--- @return number
502506function Date :get_isoweekday ()
503507 --- @type table
Original file line number Diff line number Diff line change @@ -303,6 +303,16 @@ describe('Date object', function()
303303 assert .are .same (' 2021-05-31 Mon 23:59' , date :to_string ())
304304 end )
305305
306+ it (' should get last day of the month' , function ()
307+ local date = Date .from_string (' 2024-02-02 09:00' )
308+ date = date :last_day_of_month ()
309+ assert .are .same (' 2024-02-29 Thu 09:00' , date :to_string ())
310+ date = date :add ({ month = 1 }):last_day_of_month ()
311+ assert .are .same (' 2024-03-31 Sun 09:00' , date :to_string ())
312+ date = date :set ({ month = 6 , day = 2 }):last_day_of_month ()
313+ assert .are .same (' 2024-06-30 Sun 09:00' , date :to_string ())
314+ end )
315+
306316 it (' should properly handle end of month' , function ()
307317 local date = Date .from_string (' 2021-05-12' )
308318 date = date :end_of (' month' )
You can’t perform that action at this time.
0 commit comments