All files / nanakshahi-js/lib getNanakshahiDate.js

93.75% Statements 15/16
77.77% Branches 7/9
100% Functions 1/1
93.75% Lines 15/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 773x 3x                   10x     10x         10x         10x 10x     10x 5x 5x   5x                           5x         10x   10x                                           3x  
const { leapYear, toGurmukhiNum } = require( './utils' )
const { months, weekdays } = require( './consts' )
 
/**
 * Converts given Gregorian Date to the corresponding date in the Nanakshahi Calendar
 * @param {Object} [gregorianDate=new Date()] JavaScript Date() Object
 * @return {Object} Nanakshahi Date in English and Punjabi
 * @example getNanakshahiDate( new Date() )
 */
function getNanakshahiDate( gregorianDate = new Date() ) {
  // NS Month Offsets
  const monthOffsets = [ 14, 14, 15, 15, 16, 16, 15, 15, 14, 14, 13, 12 ]
 
  // Calculate Nanakshahi Year - March 14 (1 Chet) Nanakshahi New Year
  const nsYear = gregorianDate >= new Date( gregorianDate.getFullYear(), 2, 14 )
    ? gregorianDate.getFullYear() - 1468
    : gregorianDate.getFullYear() - 1469
 
  // Check if before 535 N.S. (Nanakshahi Adoption)
  Iif ( nsYear < 535 ) {
    throw new RangeError( 'Nanakshahi Date Out of Range' )
  }
 
  // Calculate Nanakshahi Date
  let nsMonth = ( gregorianDate.getMonth() + 9 ) % 12
  const nsNextMonth = ( nsMonth + 1 ) % 12
  let nsDate
 
  if ( gregorianDate.getDate() >= monthOffsets[ nsNextMonth ] ) {
    nsMonth = nsNextMonth
    nsDate = gregorianDate.getDate() - monthOffsets[ nsNextMonth ] + 1
  } else {
    const gregorianMonths = [
      31,
      leapYear( gregorianDate.getFullYear() ) ? 29 : 28,
      31,
      30,
      31,
      30,
      31,
      31,
      30,
      31,
      30,
      31,
    ]
    nsDate = gregorianMonths[ ( gregorianDate.getMonth() + 11 ) % 12 ]
      - monthOffsets[ nsMonth ] + gregorianDate.getDate() + 1
  }
 
  // Get Day of Week
  const weekday = gregorianDate.getDay()
 
  return {
    gregorianDate,
    englishDate: {
      month: nsMonth + 1,
      monthName: months[ nsMonth ].en,
      date: nsDate,
      year: nsYear,
      day: weekdays[ weekday ].en,
      dayShort: weekdays[ weekday ].enShort,
    },
    punjabiDate: {
      month: toGurmukhiNum( nsMonth + 1 ),
      monthName: months[ nsMonth ].pa,
      date: toGurmukhiNum( nsDate ),
      year: toGurmukhiNum( nsYear ),
      day: weekdays[ weekday ].pa,
      dayShort: weekdays[ weekday ].paShort,
    },
    leapYear: leapYear( nsYear + 1469 ),
  }
}
 
module.exports = getNanakshahiDate