var g_dateFormat = "d/m/Y"; var g_dateFormatJS = "DD/MM/YYYY"; var g_timezone = "Europe/London"; var g_GoogleMapCountries = JSON.parse("[\"fr\"]"); function isDate(sDate) { var regex = new RegExp("([0-9]{4}[-](0[1-9]|1[0-2])[-]([0-2]{1}[0-9]{1}|3[0-1]{1})|([0-2]{1}[0-9]{1}|3[0-1]{1})[-](0[1-9]|1[0-2])[-][0-9]{4})"); try{ if(regex.test(sDate)) return true; return false; } catch(e){ return false; } } function convertDateForDisplay(str_date, hasHours = false) { var dateFormat = "d/m/Y"; // str_date in y-m-d => need to convert to dateFormat // get date and hours from str_date str_date = str_date.replace('T', ' '); str_date = str_date.replace('Z', ''); var date = str_date.split(" ")[0]; if(hasHours){ var hours = str_date.split(" ")[1]; var hours = hours.split(":")[0] + ":" + hours.split(":")[1]; } // split date var strDateArray = date.split("-"); // split dateFormat var dateFormatArray = dateFormat.split("/"); var separator = "/"; // if separator of dateFormat is not "/" test with "-" if(dateFormatArray.length < 2){ dateFormatArray = dateFormat.split("-"); separator = "-"; } // get index of day / month / year var indexDay = dateFormatArray.indexOf("d"); var indexMonth = dateFormatArray.indexOf("m"); var indexYear = dateFormatArray.indexOf("Y"); var dateToReturnArray = []; dateToReturnArray[indexDay] = strDateArray[2]; dateToReturnArray[indexMonth] = strDateArray[1]; dateToReturnArray[indexYear] = strDateArray[0]; var dateToReturn = dateToReturnArray.join(separator); if(hasHours){ dateToReturn = dateToReturn + " " + hours; } return dateToReturn; } function convertDateFromDisplay(str_date, hasHours = false) { // str_date in dateFormat => need to convert to y-m-d var dateFormat = "d/m/Y"; // split dateFormat var dateFormatArray = dateFormat.split("/"); var separator = "/"; // if separator of dateFormat is not "/" test with "-" if(dateFormatArray.length < 2){ dateFormatArray = dateFormat.split("-"); separator = "-"; } // get index of day / month / year var indexDay = dateFormatArray.indexOf("d"); var indexMonth = dateFormatArray.indexOf("m"); var indexYear = dateFormatArray.indexOf("Y"); // split date var strDateArray = str_date.split(" ")[0].split(separator); var dateToReturnArray = []; dateToReturnArray[indexDay] = strDateArray[2]; dateToReturnArray[indexMonth] = strDateArray[1]; dateToReturnArray[indexYear] = strDateArray[0]; var dateToReturn = dateToReturnArray.join("-"); // Manage time format if ( hasHours ) { var timeToReturn = '00:00:00'; if ( str_date.split(" ")[1] !== undefined ) { var strTimeArray = str_date.split(" ")[1].split(':'); var timeToReturnArray = []; timeToReturnArray[0] = (strTimeArray[0] !== undefined && strTimeArray[0] !== '') ? strTimeArray[0] : '00'; timeToReturnArray[1] = (strTimeArray[1] !== undefined && strTimeArray[1] !== '') ? strTimeArray[1] : '00'; timeToReturnArray[2] = (strTimeArray[2] !== undefined && strTimeArray[2] !== '') ? strTimeArray[2] : '00'; timeToReturn = timeToReturnArray.join(":"); } dateToReturn += ' ' + timeToReturn; } return dateToReturn; } function getDateYMDFormat(date,fullDate = false ){ var mm = date.getMonth() + 1; // getMonth() is zero-based var dd = date.getDate(); if(fullDate == true){ var hh = date.getHours(); var ii = date.getMinutes(); var globalDate = [date.getFullYear(), (mm>9 ? '' : '0') + mm, (dd>9 ? '' : '0') + dd ].join('-'); var globalHours = [ (hh>9 ? '' : '0') + hh, (ii>9 ? '' : '0') + ii ].join(':'); return globalDate+' '+globalHours; } return [date.getFullYear(), (mm>9 ? '' : '0') + mm, (dd>9 ? '' : '0') + dd ].join('-'); } function getLocalTime() { const now = new Date(); const formatter = new Intl.DateTimeFormat('fr-FR', { hour: 'numeric', hour12: false, minute: 'numeric', day: 'numeric', month: 'numeric', year: 'numeric', timeZone: g_timezone, }); return convertDateFromDisplay(formatter.format(now), true); } /** * add two times together * @doc https://gist.github.com/markhker/33868633a18d5bf3f8561aac5daf906f */ function addTimes (startTime, endTime) { var times = [ 0, 0, 0 ] var max = times.length var a = (startTime || '').split(':') var b = (endTime || '').split(':') // normalize time values for (var i = 0; i < max; i++) { a[i] = isNaN(parseInt(a[i])) ? 0 : parseInt(a[i]) b[i] = isNaN(parseInt(b[i])) ? 0 : parseInt(b[i]) } // store time values for (var i = 0; i < max; i++) { times[i] = a[i] + b[i] } var hours = times[0] var minutes = times[1] var seconds = times[2] if (seconds >= 60) { var m = (seconds / 60) << 0 minutes += m seconds -= 60 * m } if (minutes >= 60) { var h = (minutes / 60) << 0 hours += h minutes -= 60 * h } return ('0' + hours).slice(-2) + ':' + ('0' + minutes).slice(-2) + ':' + ('0' + seconds).slice(-2) } /** * Convert an integer to H:i * @example if 100 is given, will return "01:40" * @param int $minuteNumber * @return string */ function convertNumberOfMinuteToHour(minuteNumber) { const hours = Math.floor(minuteNumber / 60) < 10 ? "0" + Math.floor(minuteNumber / 60) : Math.floor(minuteNumber / 60); const minutes = minuteNumber % 60 < 10 ? "0" + minuteNumber % 60 : minuteNumber % 60; return hours + ":" + minutes; } /** * Convert a time H:i:s to an int (number of minutes) * @param string time * @return decimal */ function timeToNumber(time) { return moment.duration(time).asMinutes(); } /** * @param Date date */ function getNextMonth(date) { if (date.getMonth() == 11) { return new Date(date.getFullYear() + 1, 0, 1); } else { return new Date(date.getFullYear(), date.getMonth() + 1, 1); } } /** * @param Date date */ function getPreviousMonth(date) { const copyDate = new Date(date); copyDate.setDate(0); const firstDayPreviousMonth = new Date(copyDate.setDate(1)); return firstDayPreviousMonth; }