days_in_month.js (3318B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var isDateObject = require( '@stdlib/assert/is-date-object' ); 24 var isString = require( '@stdlib/assert/is-string' ).isPrimitive; 25 var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; 26 var lowercase = require( '@stdlib/string/lowercase' ); 27 var isLeapYear = require( '@stdlib/assert/is-leap-year' ); 28 var DAYS_IN_MONTH = require( './days.json' ); 29 30 31 // MAIN // 32 33 /** 34 * Returns the number of days in a month. 35 * 36 * @param {(string|Date|integer)} [month] - month (or Date) 37 * @param {integer} [year] - year 38 * @throws {TypeError} first argument must be either a string, integer, or `Date` object 39 * @throws {Error} must provide a recognized month 40 * @throws {RangeError} an integer month argument must be on the interval `[1,12]` 41 * @throws {TypeError} second argument must be an integer 42 * @returns {integer} days in a month 43 * 44 * @example 45 * var num = daysInMonth(); 46 * // returns <number> 47 * 48 * @example 49 * var num = daysInMonth( 2 ); 50 * // returns <number> 51 * 52 * @example 53 * var num = daysInMonth( 2, 2016 ); 54 * // returns 29 55 * 56 * @example 57 * var num = daysInMonth( 2, 2017 ); 58 * // returns 28 59 */ 60 function daysInMonth( month, year ) { 61 var days; 62 var mon; 63 var yr; 64 var d; 65 if ( arguments.length === 0 ) { 66 // Note: cannot cache as application may cross over into a new year: 67 d = new Date(); 68 mon = d.getMonth() + 1; // zero-based 69 yr = d.getFullYear(); 70 } else if ( arguments.length === 1 ) { 71 if ( isDateObject( month ) ) { 72 d = month; 73 mon = d.getMonth() + 1; // zero-based 74 yr = d.getFullYear(); 75 } else if ( isString( month ) || isInteger( month ) ) { 76 // Note: cannot cache as application may cross over into a new year: 77 yr = ( new Date() ).getFullYear(); 78 mon = month; 79 } else { 80 throw new TypeError( 'invalid argument. First argument must be either a string, integer, or `Date` object. Value: `'+month+'`.' ); 81 } 82 } else { 83 if ( !isString( month ) && !isInteger( month ) ) { 84 throw new TypeError( 'invalid argument. First argument must be either a string or integer. Value: `'+month+'`.' ); 85 } 86 if ( !isInteger( year ) ) { 87 throw new TypeError( 'invalid argument. Second argument must be an integer. Value: `'+year+'`.' ); 88 } 89 mon = month; 90 yr = year; 91 } 92 if ( isInteger( mon ) && (mon < 1 || mon > 12) ) { 93 throw new RangeError( 'invalid argument. An integer month value must be on the interval `[1,12]`. Value: `'+mon+'`.' ); 94 } 95 mon = lowercase( mon.toString() ); 96 days = DAYS_IN_MONTH[ mon ]; 97 if ( days === void 0 ) { 98 throw new Error( 'invalid argument. Must provide a recognized month. Value: `'+mon+'`.' ); 99 } 100 if ( days === 28 && isLeapYear( yr ) ) { 101 days += 1; 102 } 103 return days; 104 } 105 106 107 // EXPORTS // 108 109 module.exports = daysInMonth;