Skip to content

Instantly share code, notes, and snippets.

@nowri
Last active May 8, 2017 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nowri/0b450ccbccf68a254f984df109fd993e to your computer and use it in GitHub Desktop.
Save nowri/0b450ccbccf68a254f984df109fd993e to your computer and use it in GitHub Desktop.
inputフォームのvalueをHH:mm形式にフォーマットする
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