Last active
May 8, 2017 20:16
-
-
Save nowri/0b450ccbccf68a254f984df109fd993e to your computer and use it in GitHub Desktop.
inputフォームのvalueをHH:mm形式にフォーマットする
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import moment from 'moment' | |
export default { | |
formInputTime (val = '') { | |
let h, m, ar | |
val = val.replace(/(^\s+)|(\s+$)/g, '').replace(/\n/g, '') | |
ar = val.split(':') | |
if (ar.length === 2) { | |
h = ar[0] | |
m = ar[1] | |
} else { | |
if (val.match(/\d{3}/) && val.length === 3) { | |
h = val.substr(0, 1) | |
m = val.substr(1, 2) | |
} else if (val.match(/\d{4}/)) { | |
h = val.substr(0, 2) | |
m = val.substr(2, 2) | |
} | |
} | |
if ((h || h === 0) && (m || m === 0)) { | |
return moment().hour(h).minute(m).format('HH:mm') | |
} else { | |
return '' | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment