UNPKG

clock-format-utility

Version:

Utility that can detect clock format (12/24 hour) and can set clock format for moment

1 lines 2.1 kB
Object.defineProperty(exports,"__esModule",{value:true});exports.setMomentLocaleClockFormat=exports.replaceFormatInString=exports.setMomentLocale=undefined;var _extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key];}}}return target;};var _detect=require('./detect');var setMomentLocale=exports.setMomentLocale=function setMomentLocale(){var locale=arguments.length>0&&arguments[0]!==undefined?arguments[0]:(0,_detect.detectDeviceLocale)();return require('moment').locale(locale);};var replaceFormatInString=exports.replaceFormatInString=function replaceFormatInString(oldValue){var clockFormat=arguments.length>1&&arguments[1]!==undefined?arguments[1]:'24';return clockFormat==='24'?oldValue.replace('h:mm A','HH:mm'):oldValue.replace('HH:mm','h:mm A');};var setMomentLocaleClockFormat=exports.setMomentLocaleClockFormat=function setMomentLocaleClockFormat(){var clockFormat=arguments.length>0&&arguments[0]!==undefined?arguments[0]:(0,_detect.detectDeviceClockFormat)();var locale=arguments.length>1&&arguments[1]!==undefined?arguments[1]:require('moment').locale();var moment=require('moment'),longDateFormat=moment.localeData(locale)._longDateFormat;if(clockFormat==='24'){moment.updateLocale(locale,{longDateFormat:_extends({},longDateFormat,{LT:'HH:mm',LTS:'HH:mm:ss',LLL:longDateFormat.LLL&&replaceFormatInString(longDateFormat.LLL),LLLL:longDateFormat.LLLL&&replaceFormatInString(longDateFormat.LLLL),lll:longDateFormat.lll&&replaceFormatInString(longDateFormat.lll),llll:longDateFormat.llll&&replaceFormatInString(longDateFormat.llll)})});}else{moment.updateLocale(locale,{longDateFormat:_extends({},longDateFormat,{LT:'h:mm A',LTS:'h:mm:ss A',LLL:longDateFormat.LLL&&replaceFormatInString(longDateFormat.LLL,'12'),LLLL:longDateFormat.LLLL&&replaceFormatInString(longDateFormat.LLLL,'12'),lll:longDateFormat.lll&&replaceFormatInString(longDateFormat.lll,'12'),llll:longDateFormat.llll&&replaceFormatInString(longDateFormat.llll,'12')})});}};