Skip to content

Instantly share code, notes, and snippets.

@ryansutc
Created December 22, 2020 19:23
Show Gist options
  • Save ryansutc/fa61f59af1f03bb1ad8d126d61af5ad1 to your computer and use it in GitHub Desktop.
Save ryansutc/fa61f59af1f03bb1ad8d126d61af5ad1 to your computer and use it in GitHub Desktop.
A good datetime formatter Curry Example
// Helper function to curry 2 arg functions
const curry = f => b => a => f(a,b);
// oh look, a 2 arg function!
function formatDateForLabeling(dateString, format='yyyymmdd') {
let d = new Date(0);
d.setUTCSeconds(dateString);
if(format === 'mmddyyyy') {
return new Intl.DateTimeFormat('en-US', {timeZone: 'UTC'}).format(d);
}
else if(format === 'ddmmyyyy') {
return new Intl.DateTimeFormat('en-GB', {timeZone: 'UTC'}).format(d); // UTC ex: '24/9/2020'
}
else if(format === 'yyyymmdd') {
return d.toISOString().split('T')[0]
}
}
let dateFormat = 'yyyymmdd'; //'ddmmyyyy';
let UTCexample = 1586773746;
const curriedFormatDateForLabeling = curry(formatDateForLabeling);
const simpleFormatDate = curriedFormatDateForLabeling(dateFormat)
console.log(simpleFormatDate(UTCexample))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment