Dart March 30 days

Multi tool use
Multi tool use


Dart March 30 days



Small question :



I calculate the number of days in a given month like so :


new DateTime(2018, month+1, 1).difference(new DateTime(2018,month,1)).inDays



All is working fine, februari gives me 28 days, all months are correctly 30/31 days except for March, which gives me 30 days instead of 31.


februari


28


30/31


March


30


31



Has anyone seen this before and perhaps an explanation?



Thanks.





Probably DST. Stupid DST. We need to get rid of DST. Edit: probably if you use UTC instead of local timezone, it'd work right.
– Randal Schwartz
Jul 2 at 1:45





1 Answer
1



I believe it's because of daylight-saving, try new DateTime.utc(), the below code outputs the correct values


for(int month = 1; month < 13; month++) {

int daysInMonth = new DateTime.utc(2018, month+1,1).difference(new DateTime.utc(2018,month,1)).inDays;

print(daysInMonth);
}



"When dealing with dates or historic events prefer to use UTC DateTimes, since they are unaffected by daylight-saving changes and are unaffected by the local timezone."





Quote them instead of posting a link.
– Hearen
Jul 2 at 2:42






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

BbcPtAHIx,m9EzjWYKVywbMAVgSM vcpds4zgfhmEqbt nB0H q qKmbN YkF0UMlU
1mSzH 5kjfqZ Wvufc7iLWSYppnv,V,Us0Xr,t9,mVln3Pr 5JeOUG,8cKbgVwev,z9WE,7 1lTjK2DgH9G99I6Ei7CHMR PN Ix5u DR4GNh2L

Popular posts from this blog

Rothschild family

Boo (programming language)