src/tokenizer/time.js
function nthWeekDay(countDays, weekDay, year, month, day) {
  let countedDays = 0
  if (countDays >= 0) {
    month = month || 1
    let monthStr = String(month)
    if (monthStr.length === 1) { monthStr = "0" + monthStr }
    day = day || 1
    let dayStr = String(day)
    if (dayStr.length === 1) { dayStr = "0" + dayStr }
    // We stay a minute off midnight to avoid dealing with leap seconds.
    let timestamp = +new Date(`${year}-${monthStr}-${dayStr}T00:01:00Z`)
    let time
    for (let i = 0; i < 366; i++) {
      time = new Date(timestamp)
      if (time.getDay() === weekDay) {countedDays++}
      if (countedDays === countDays) {break}
      timestamp += 24 * 3600 * 1000
    }
    return time
  } else if (countDays < 0) {
    countDays = -countDays
    month = month || 12
    let monthStr = String(month)
    if (monthStr.length === 1) { monthStr = "0" + monthStr }
    day = day || 31
    let dayStr = String(day)
    if (dayStr.length === 1) { dayStr = "0" + dayStr }
    // Starting moment, ms.
    let timestamp = +new Date(`${year}-${monthStr}-${dayStr}T00:01:00Z`)
    let time
    for (let i = 366; i >= 0; i--) {
      time = new Date(timestamp)
      if (time.getDay() === weekDay) {countedDays++}
      if (countedDays === countDays) {break}
      timestamp -= 24 * 3600 * 1000
    }
    return time
  }
}
function nthWeekDay(countDays, weekDay, year, month, day) {
  let countedDays = 0
  if (countDays >= 0) {
    month = month || 1
    let monthStr = String(month)
    if (monthStr.length === 1) { monthStr = "0" + monthStr }
    day = day || 1
    let dayStr = String(day)
    if (dayStr.length === 1) { dayStr = "0" + dayStr }
    // We stay a minute off midnight to avoid dealing with leap seconds.
    let timestamp = +new Date(`${year}-${monthStr}-${dayStr}T00:01:00Z`)
    let time
    for (let i = 0; i < 366; i++) {
      time = new Date(timestamp)
      if (time.getDay() === weekDay) {countedDays++}
      if (countedDays === countDays) {break}
      timestamp += 24 * 3600 * 1000
    }
    return time
  } else if (countDays < 0) {
    countDays = -countDays
    month = month || 12
    let monthStr = String(month)
    if (monthStr.length === 1) { monthStr = "0" + monthStr }
    day = day || 31
    let dayStr = String(day)
    if (dayStr.length === 1) { dayStr = "0" + dayStr }
    // Starting moment, ms.
    let timestamp = +new Date(`${year}-${monthStr}-${dayStr}T00:01:00Z`)
    let time
    for (let i = 366; i >= 0; i--) {
      time = new Date(timestamp)
      if (time.getDay() === weekDay) {countedDays++}
      if (countedDays === countDays) {break}
      timestamp -= 24 * 3600 * 1000
    }
    return time
  }
}
function nthWeekDay ( countDays , weekDay , year , month , day ) { let countedDays = 0 if ( countDays >= 0 ) { month = month || 1 let monthStr = String ( month ) if ( monthStr . length === 1 ) { monthStr = "0" + monthStr } day = day || 1 let dayStr = String ( day ) if ( dayStr . length === 1 ) { dayStr = "0" + dayStr } // We stay a minute off midnight to avoid dealing with leap seconds. let timestamp = + new Date ( ` ${ year } ${ monthStr } ${ dayStr } ` ) let time for ( let i = 0 ; i < 366 ; i ++ ) { time = new Date ( timestamp ) if ( time . getDay ( ) === weekDay ) { countedDays ++ } if ( countedDays === countDays ) { break } timestamp += 24 * 3600 * 1000 } return time } else if ( countDays < 0 ) { countDays = - countDays month = month || 12 let monthStr = String ( month ) if ( monthStr . length === 1 ) { monthStr = "0" + monthStr } day = day || 31 let dayStr = String ( day ) if ( dayStr . length === 1 ) { dayStr = "0" + dayStr } // Starting moment, ms. let timestamp = + new Date ( ` ${ year } ${ monthStr } ${ dayStr } ` ) let time for ( let i = 366 ; i >= 0 ; i -- ) { time = new Date ( timestamp ) if ( time . getDay ( ) === weekDay ) { countedDays ++ } if ( countedDays === countDays ) { break } timestamp -= 24 * 3600 * 1000 } return time } }
eg. second friday of 2015
eg . second friday of 2015