Skip to content

Instantly share code, notes, and snippets.

@suissa
Last active April 5, 2017 07:59
Show Gist options
  • Save suissa/599b098ffbb9e8a77d8b921569df8e84 to your computer and use it in GitHub Desktop.
Save suissa/599b098ffbb9e8a77d8b921569df8e84 to your computer and use it in GitHub Desktop.
Transforma horas com PM em horas normais.
const periods = [ `am`, `pm` ]
const getTimeWitoutPeriod = ( time ) => time.split( /(am|pm)/ )[0]
const getHoursAndMinutes = ( time ) => time.split( `:` )
const testIfHourIs00 = ( time ) => time.startsWith( `00:` )
const timePuls12Hours = ( h, m ) => parseInt( h ) + 12 + `:${m}`
const removingTimeFrom = ( time ) => time.split(/[0-9]/)
const timeIfIsIn = ( periods ) => ( time ) =>
periods.includes( time )
const getOnlyThePeriod = ( time ) =>
time.filter( timeIfIsIn( periods ) )[0].toLowerCase()
const getPeriodFrom = ( hour ) =>
getOnlyThePeriod( removingTimeFrom( hour ) )
const getMorningTime = ( hour ) =>
transformAMToNormal( getTimeWitoutPeriod( hour ) )
const getEveningTime = ( hour ) =>
transformPMToNormal( getTimeWitoutPeriod( hour ) )
const transformAMToNormal = ( time ) =>
( testIfHourIs00( time ) )
? timePuls12Hours( ...getHoursAndMinutes( time ) )
: time
const transformPMToNormal = ( time ) =>
( testIfHourIs00( time ) )
? time
: timePuls12Hours( ...getHoursAndMinutes( time ) )
const transform = ( hour ) =>
( getPeriodFrom( hour ) === `am` )
? getMorningTime( hour )
: getEveningTime( hour )
const hourPM = `05:01pm`
console.log( `Transforme ${hourPM}: `, transform( hourPM ) )
const hourPM2 = `00:01pm`
console.log( `Transforme ${hourPM2}: `, transform( hourPM2 ) )
const hourAM = `00:01am`
console.log( `Transforme ${hourAM}: `, transform( hourAM ) )
const hourAM1 = `05:01am`
console.log( `Transforme ${hourAM1}: `, transform( hourAM1 ) )
@higordiego
Copy link

ui :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment