Skip to content

Instantly share code, notes, and snippets.

@stigok
Last active June 4, 2017 23:30
Show Gist options
  • Save stigok/0fd8b7c9ad953f22237eb5c535c3d1e9 to your computer and use it in GitHub Desktop.
Save stigok/0fd8b7c9ad953f22237eb5c535c3d1e9 to your computer and use it in GitHub Desktop.
Norwegian date and time strings with leading zero
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(':')
}
}
@stigok
Copy link
Author

stigok commented Jun 4, 2017

var d = parsedate(new Date())

returns

{ parts: 
   { year: '2017',
     month: '06',
     day: '04',
     hour: '23',
     minute: '12',
     second: '42',
     undefined: '491' },
  dateString: '04/06/17',
  timeString: '23:12:42:91' }

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