ember.jsinternationalizationember-i18n

Converting between 12 hour and 24 hour clock with i18n in ember.js


I wondered if there is a way to convert between a 12-hour and 24-hour clock using i18n and ember.js?

I18n does have the DATE-object with meridians described under. But I'm not sure if conversion between 24-hour and 12-hour times are supported.

var DATE = { day_names: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"], abbr_day_names: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], month_names: [null, "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"], abbr_month_names: [null, "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], meridian: ["AM", "PM"] };

I allready use i18n for the translation part of the internationalization of my site. So it would be nice to not need an additional library for times.


Solution

  • I ended up solving the simplified problem of choosing between 12 and 24 hour time using separate handlebars dates with I18n.l. Then I specified that format so the date objects would get formatted in that way in the translations.js

    For English locale etc. I use the 12-hour clock

    time: {
        formats: {
            short: '%-I:%M %p', // hh:mm tt
        },
    }
    

    For Norwegian, Swedish locale ect. I use the 24-hour clock

    time: {
        formats: {
             short: '%-H:%M', // hh:mm
        }
    }
    

    For languages with different delimiters and meridians and names for weekdays ect. you must also specify those and not use the default DATE object that is given in the i18n.js file.

    The accepted formats in standard i18n-js are: