UNPKG

@vimeo/iris

Version:
91 lines (87 loc) 2.73 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var DATE_FORMAT_OPTIONS = { year: 'numeric', month: '2-digit', day: '2-digit', }; var DD_MM_YY_FORMAT = 'DD.MM.YYYY'; var DEFAULT_LANGUAGE = 'default'; // Converts date to ISO 8601 format for Date constuctor: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date var convertDDMMYYYToYYYYMMDD = function (date) { return date.split('.').reverse().join('.'); }; var formatter = (function () { try { return window.Intl ? new Intl.DateTimeFormat(DEFAULT_LANGUAGE, DATE_FORMAT_OPTIONS) : null; } catch (e) { return null; } })(); function formatDate(date) { var formattedDate = formatter ? formatter.format(date) : date.toLocaleDateString(); return formattedDate; } function getDateFormat() { var sample = formatDate(new Date(1970, 11, 31)); var mm = 0; var dd = 1; var yy = 2; var mi = sample.indexOf('12'); var di = sample.indexOf('31'); var yi = sample.indexOf('1970'); // Put fields in format order. Otherwise fall back to mm/dd/yyyy if (yi >= 0 && mi >= 0 && di >= 0) { mm = (mi > yi ? 1 : 0) + (mi > di ? 1 : 0); dd = (di > yi ? 1 : 0) + (di > mi ? 1 : 0); yy = (yi > mi ? 1 : 0) + (yi > di ? 1 : 0); } var r = []; r[yy] = 'YYYY'; r[mm] = 'MM'; r[dd] = 'DD'; var match = sample.match(/[-.] ?/); return r.join(match ? match[0] : '/'); } function getDateFormatRegex(format) { var f; // If there is no formatter we can't garuntee a correct regex // Allow any input - for ie11 if (formatter) { f = format.replace('DD', '\\d{2}'); f = f.replace('MM', '\\d{2}'); f = f.replace('YYYY', '\\d{4}'); } else { f = '.*'; } return new RegExp("^".concat(f, "$")); } function getMonthFromDate(dateObj, translation) { var monthNames = [ translation.months.january, translation.months.february, translation.months.march, translation.months.april, translation.months.may, translation.months.june, translation.months.july, translation.months.august, translation.months.september, translation.months.october, translation.months.november, translation.months.december, ]; return monthNames[dateObj.getMonth()]; } exports.DD_MM_YY_FORMAT = DD_MM_YY_FORMAT; exports.convertDDMMYYYToYYYYMMDD = convertDDMMYYYToYYYYMMDD; exports.formatDate = formatDate; exports.getDateFormat = getDateFormat; exports.getDateFormatRegex = getDateFormatRegex; exports.getMonthFromDate = getMonthFromDate;