Skip to content

Instantly share code, notes, and snippets.

@lubien
Forked from suissa/transformPMHourToNormal.js
Last active April 5, 2017 11:18
Show Gist options
  • Save lubien/70b0743a5ed13ab49b7e17dad02b139f to your computer and use it in GitHub Desktop.
Save lubien/70b0743a5ed13ab49b7e17dad02b139f to your computer and use it in GitHub Desktop.
Transform 12-Hour format to 24 hour. Run: https://repl.it/Gvco/2
const {
pipe, match, drop, head, range, map, identity, add
, equals, last, join, take, ifElse, adjust, always
} = require('ramda')
const leftPad = require('left-pad')
const leftPadZero = size => x => leftPad(x, size, '0')
const clockToFullHours =
pipe(
match(/^(\d{1,2}):(\d{2})(\w{2})$/i)
, drop(1)
, ifElse(pipe(last, equals('pm')))
(ifElse(pipe(head, equals('12')))
(adjust(always(0), 0))
(adjust(add(12), 0)))
(identity)
, take(2)
, map(leftPadZero(2))
, join(':')
)
console.log(
pipe(
map(h => [`${h}:00am`, `${h}:00pm`])
, map(map(clockToFullHours)
)
)(range(0, 13))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment