import toDate from '../toDate/index' ; import isValid from '../isValid/index' ; import addLeadingZeros from '../_lib/addLeadingZeros/index' ; export interface FormatISO9075Options { format ? : 'extended' | 'basic' ; representation ? : 'complete' | 'date' | 'time' ; } name category summary description param { Date | Number } date param options param { 'extended' | 'basic' } options . format param { 'complete' | 'date' | 'time' } options . representation returns { String } throws throws throws throws example example example example export default function formatISO9075 ( dirtyDate , dirtyOptions ? ) { if ( arguments . length < 1 ) { throw new TypeError ( `1 argument required, but only ${ arguments . length } present` ) ; } const originalDate = toDate ( dirtyDate ) ; if ( ! isValid ( originalDate ) ) { throw new RangeError ( 'Invalid time value' ) ; } const options = dirtyOptions || { } ; const format = options . format == null ? 'extended' : String ( options . format ) ; const representation = options . representation == null ? 'complete' : String ( options . representation ) ; if ( format !== 'extended' && format !== 'basic' ) { throw new RangeError ( "format must be 'extended' or 'basic'" ) ; } if ( representation !== 'date' && representation !== 'time' && representation !== 'complete' ) { throw new RangeError ( "representation must be 'date', 'time', or 'complete'" ) ; } let result = '' ; const dateDelimiter = format === 'extended' ? '-' : '' ; const timeDelimiter = format === 'extended' ? ':' : '' ; if ( representation !== 'time' ) { const day = addLeadingZeros ( originalDate . getDate ( ) , 2 ) ; const month = addLeadingZeros ( originalDate . getMonth ( ) + 1 , 2 ) ; const year = addLeadingZeros ( originalDate . getFullYear ( ) , 4 ) ; result = `${ year }${ dateDelimiter }${ month }${ dateDelimiter }${ day }` ; } if ( representation !== 'date' ) { const hour = addLeadingZeros ( originalDate . getHours ( ) , 2 ) ; const minute = addLeadingZeros ( originalDate . getMinutes ( ) , 2 ) ; const second = addLeadingZeros ( originalDate . getSeconds ( ) , 2 ) ; const separator = result === '' ? '' : ' ' ; result = `${ result }${ separator }${ hour }${ timeDelimiter }${ minute }${ timeDelimiter }${ second }` ; } return result ; }