Skip to content

Instantly share code, notes, and snippets.

@sukima
Created April 7, 2015 16:21
Show Gist options
  • Save sukima/c840a2bddbd7d8cab809 to your computer and use it in GitHub Desktop.
Save sukima/c840a2bddbd7d8cab809 to your computer and use it in GitHub Desktop.
// Convert the format string from moment's "L" to a format usable by
// [bootstrap-datepicker][1].
//
// Call it with:
//
// momentFormatToDatePicker(moment.localeData().longDateFormat("L")).
//
// [1]: https://bootstrap-datepicker.readthedocs.org/en/latest/options.html#format
function momentFormatToDatePicker(format) {
// The actual translation is more detailed:
// | Moment | Date Picker | Used by "L" |
// |--------|-------------|-------------|
// | MMMM | MM | ✘ No |
// | MMM | M | ✘ No |
// | MM | mm | ✔ Yes |
// | M | m | ✘ No |
// | Mo | m | ✘ No |
// | DDDD | DD | ✘ No |
// | DDD | D | ✘ No |
// | DDDo | D | ✘ No |
// | DD | dd | ✔ Yes |
// | D | d | ✔ Yes |
// | Do | d | ✘ No |
// | YYYY | yyyy | ✔ Yes |
// | YY | yy | ✔ Yes |
// However, of the 72 languages supported by Moment none of them use the
// exetended formats for their localized "L" and of the formats used a simple
// toLowerCase() covers the conversion we need. This chart left for future
// concideration if more advanced conversions are required.
return format.toLowerCase();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment