Last active
June 4, 2017 23:30
-
-
Save stigok/0fd8b7c9ad953f22237eb5c535c3d1e9 to your computer and use it in GitHub Desktop.
Norwegian date and time strings with leading zero
This file contains hidden or 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
function datestrings (date) { | |
const format = ['year', 'month', 'day', 'hour', 'minute', 'second'] | |
const parts = date | |
.toISOString() | |
.split(/[^\d]/g) | |
.slice(0, format.length) | |
.reduce((obj, part, i) => { | |
obj[format[i]] = part | |
return obj | |
}, {}) | |
const withLeadingZero = (num) => ('0' + num).slice(-2) | |
const arr = Object.keys(parts).map(key => parts[key]) | |
return { | |
parts, | |
dateString: arr.splice(0, 3).reverse().map(withLeadingZero).join('/'), | |
timeString: arr.map(withLeadingZero).join(':') | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
returns