@geneui/components
Version:
The Gene UI components library designed for BI tools
47 lines (42 loc) • 2.25 kB
JavaScript
/* Localization (https://developer.mozilla.org/en-US/docs/Mozilla/Localization/Web_Localizability/Creating_localizable_web_applications)
* The localization problem includes languages(i18n),
* date & number formating, currency and document direction.
* The project itself did not include any text and currency.
* Document direction problem is solved on the markup level.
* Here will be helpers for localizing date and number formats.
*/
// For IE11
const getParsedString = date => [].filter.call(date, (v, i) => date.charCodeAt(i) !== 8206).join('');
const getBrowserTimeFormat = function () {
let includeSeconds = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
const date = new Date(2013, 11, 31, 23, 5, 8);
const dateString = date.toLocaleTimeString();
const parsedString = getParsedString(dateString);
let timeFormat;
if (!dateString.match(/23/i)) {
timeFormat = parsedString.replace(/PM|AM/, '').replace('11', 'hh').replace('05', 'mm').replace('08', includeSeconds ? 'ss' : '').trim();
timeFormat = includeSeconds ? "".concat(timeFormat, " A") : "".concat(timeFormat.slice(0, -1), " A");
} else {
timeFormat = parsedString.replace('23', 'HH').replace('05', 'mm').replace('08', includeSeconds ? 'ss' : '');
timeFormat = includeSeconds ? timeFormat : timeFormat.trim().slice(0, -1);
}
return timeFormat;
};
/**
* This function is used to get date format depending on browser settings.
* @param {boolean} withTime - returns time format also
* @param {boolean} includeSeconds - shows seconds
* @returns {string} Date format.
*/
const getBrowserDateFormat = function () {
let withTime = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
let includeSeconds = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
const browserDate = new Date(2013, 11, 31).toLocaleDateString();
const parsedString = getParsedString(browserDate);
let dateFormat = parsedString.replace('31', 'DD').replace('12', 'MM').replace('2013', 'YYYY');
if (withTime) {
dateFormat = "".concat(dateFormat, " ").concat(getBrowserTimeFormat(includeSeconds));
}
return dateFormat;
};
export { getBrowserTimeFormat as a, getBrowserDateFormat as g };