File tree 3 files changed +16
-2
lines changed
3 files changed +16
-2
lines changed Original file line number Diff line number Diff line change 335
335
336
336
function Calendar :forward ()
337
337
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 })
339
339
self :render ()
340
340
vim .fn .cursor (2 , 1 )
341
341
vim .fn .search (' 01' )
344
344
345
345
function Calendar :backward ()
346
346
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 ( )
348
348
self :render ()
349
349
vim .fn .cursor (8 , 0 )
350
350
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)
498
498
return self
499
499
end
500
500
501
+ function Date :last_day_of_month ()
502
+ return self :set ({ day = Date ._days_of_month (os_date (self .timestamp )) })
503
+ end
504
+
501
505
--- @return number
502
506
function Date :get_isoweekday ()
503
507
--- @type table
Original file line number Diff line number Diff line change @@ -303,6 +303,16 @@ describe('Date object', function()
303
303
assert .are .same (' 2021-05-31 Mon 23:59' , date :to_string ())
304
304
end )
305
305
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
+
306
316
it (' should properly handle end of month' , function ()
307
317
local date = Date .from_string (' 2021-05-12' )
308
318
date = date :end_of (' month' )
You can’t perform that action at this time.
0 commit comments