tomaszkubacki tomaszkubacki - 13 days ago 6
Dart Question

How to get first day of previous month Datetime with Dartlang

I want to get first day of previous month in Dart as DateTime.
Below code is working correctly even for x = 1 (passing 0 as month)

print(new DateTime(2016,x-1,1));


but is it by design or I should not relay on it ?

lrn lrn
Answer

It is by design. The DateTime constructor you are using allows overflow and underflow on both days and months. A month value less than one means a month prior to January, which is a month in a previous year. Likewise a day value of less than one is a day in a previous month.

Comments