Skip to content

Instantly share code, notes, and snippets.

@asafge
Last active August 29, 2015 14:01
Show Gist options
  • Save asafge/0b13c5066d06ae9a4446 to your computer and use it in GitHub Desktop.
Save asafge/0b13c5066d06ae9a4446 to your computer and use it in GitHub Desktop.
Convert a python/strfdate date/time format to moment.js's format
var pythonToJsFormats = Object.freeze({
'%a': 'ddd',
'%A': 'dddd',
'%w': 'd',
'%d': 'DD',
'%b': 'MMM',
'%B': 'MMMM',
'%m': 'MM',
'%y': 'YY',
'%Y': 'YYYY',
'%H': 'HH',
'%I': 'hh',
'%p': 'A',
'%M': 'mm',
'%S': 'ss',
'%f': 'SSS',
'%z': 'ZZ',
'%Z': 'z',
'%j': 'DDDD',
'%U': 'ww', // Week day of the year, Sunday first - not supported
'%W': 'ww', // Week day of the year, Monday first
'%c': 'ddd MMM DD HH:mm:ss YYYY',
'%x': 'MM/DD/YYYY',
'%X': 'HH:mm:ss',
'%%': '%'
});
var convertFormat = function(format) {
var converted = format;
for(var name in this.pythonToJsFormats) {
if (this.pythonToJsFormats.hasOwnProperty(name)) {
converted = converted.split(name).join(this.pythonToJsFormats[name]);
}
}
return converted;
};
convertFormat('%Y-%m-%d %H:%M:%S'); // --> 'YYYY-MM-DD HH:mm:ss'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment