toDateTime method

DateTime toDateTime()

Converts the NepaliDateTime to corresponding DateTime.

Can be used to convert BS to AD.

Implementation

DateTime toDateTime() {
  // Setting english reference to 1913/1/1, which converts to 1969/9/18
  var englishYear = 1913;
  var englishMonth = 1;
  var englishDay = 1;

  var difference = _nepaliDateDifference(
    NepaliDateTime(year, month, day),
    NepaliDateTime(1969, 9, 18),
  );

  // Getting english year until the difference remains less than 365
  while (difference >= (_isLeapYear(englishYear) ? 366 : 365)) {
    difference = difference - (_isLeapYear(englishYear) ? 366 : 365);
    englishYear++;
  }

  // Getting english month until the difference remains less than 31
  final monthDays =
      _isLeapYear(englishYear) ? _englishLeapMonths : _englishMonths;
  var i = 0;
  while (difference >= monthDays[i]) {
    englishMonth++;
    difference -= monthDays[i];
    i++;
  }

  // Remaining days is the nepaliDateTime;
  englishDay += difference;

  return DateTime(
    englishYear,
    englishMonth,
    englishDay,
    hour,
    minute,
    second,
    millisecond,
    microsecond,
  );
}