If there was a poll to find that one little feature every C# developer would love to have in .Net, then it is most likely the ability to store Date and Time individually. For years now, we had to use DateTime to represent Date when the Time part of the object had to be ignored. The same thing happened when we wanted to save time and had to ignore the Date of the DateTime component.
DateOnly
- var dateOnlyFirst = new DateOnly(2020, 2, 16);
- var dateOnlySecond = DateOnly.FromDateTime(DateTime.Now);
- Console.WriteLine(dateOnlyFirst);
- Console.WriteLine(dateOnlySecond);
- // Output
- 16-02-2020
- 04-06-2021
- private readonly int _dayNumber;
- // Maps to Jan 1st year 1
- private const int MinDayNumber = 0;
- // Maps to December 31 year 9999. The value calculated from "new DateTime(9999, 12, 31).Ticks / TimeSpan.TicksPerDay"
- private const int MaxDayNumber = 3_652_058;
- public DateOnly(int year, int month, int day) => _dayNumber = DayNumberFromDateTime(new DateTime(year, month, day));
- public static DateOnly FromDateTime(DateTime dateTime) => new DateOnly(DayNumberFromDateTime(dateTime));
- private static int DayNumberFromDateTime(DateTime dt) => (int)(dt.Ticks / TimeSpan.TicksPerDay);

khushboo kumariPosted Jun 6, 2021, 3:16 PM
Learn programming on KK CodesWhatsApp : https://chat.whatsapp.com/BEuzFKt1xh25UvKeykrVSj Telegram : https://t.me/joinchat/m3j4r8I2c80wMWE1 Facebook : https://www.facebook.com/groups/243228876878806 Website : https://kkcodes.in/ YouTube : https://www.youtube.com/channel/UC2mNapkDneELS_N-m_zNvpQ Instagram : https://www.instagram.com/kkcodes.in/ LinkedIn : https://www.linkedin.com/groups/12523807/