
function stringDatePadDigits(num, digits) {
  return num.toString().padStart(digits, '0');
}

function formatDatetoStringDateTime(uneDate){
  let temp = '';
  if (uneDate) {
    temp += uneDate.getFullYear();
    temp += ''+stringDatePadDigits(uneDate.getMonth()+1,2);
    temp += ''+stringDatePadDigits(uneDate.getDate(),2);
    temp += ''+stringDatePadDigits(uneDate.getHours(),2);
    temp += ''+stringDatePadDigits(uneDate.getMinutes(),2);
  }
  temp += '000000000000';
  return temp.substring(0,12);
}

function formatTimeFromStringDate(uneStringDate) {
  let retour ='';
  if ('0000' == uneStringDate.substring(8,12)) retour='-';
  else retour = uneStringDate.substring(8,10)+':'+uneStringDate.substring(10,12);
  return retour;
}

function formatDatetoStringDate(uneDate){
  let temp = formatDatetoStringDateTime(uneDate);
  return temp.substring(0,8);
  return temp.substring(0,8)+'0000';
}

function formatLocaleDateFromStringDate(uneStringDate) {
  if (uneStringDate.substring(0,8) == '00000000') return '-';
  if (uneStringDate.substring(0,8) == '99999999') return '-';
  return uneStringDate.substring(6,8) +'/'+uneStringDate.substring(4,6)+'/'+uneStringDate.substring(0,4);
}

function stringdateToDate(uneStringDate) {
  let retour = new Date();
  retour.setFullYear(parseInt(uneStringDate.substring(0,4)));
  retour.setMonth(parseInt(uneStringDate.substring(4,6))-1);
  retour.setDate(parseInt(uneStringDate.substring(6,8)));

  retour = new Date(parseInt(uneStringDate.substring(0,4)), parseInt(uneStringDate.substring(4,6))-1, parseInt(uneStringDate.substring(6,8)))
  return retour;
}

function stringdateGetDayOfYear(uneStringDate){
  let currentYear = uneStringDate.substring(0,4);
  let currentDate = stringdateToDate(uneStringDate);
  let startDate = new Date(parseInt(currentYear), 0, 4);
  return currentYear+stringDatePadDigits(Math.floor((currentDate - startDate) / (24 * 60 * 60 * 1000)),3);
}

function stringdateGetWeekOfYear(uneStringDate) {
  let currentDayOfYear = dateGetPrevMonday(stringdateToDate(uneStringDate));
  let startDayOfYear = dateGetPrevMonday(stringdateToDate(currentDayOfYear.getUTCFullYear()+'0104')); 
  let weekNo = Math.ceil(((currentDayOfYear-startDayOfYear) / 86400000 + 1) / 7);;
  return startDayOfYear.getUTCFullYear() * 100 + weekNo;
}

function stringdateGetWeek(uneStringDate) {
  myError('Fonction Obsolète : '+arguments.callee.name);
  return stringdateGetWeekOfYear(uneStringDate);
}

function dateGetToday(){
  let today = now.getDate();
  today.setHours(0, 0, 0, 0);
  return today;
}

function dateGetTomorrow(){
  let now = new Date();
  let today = now.getDate();
  let tomorrow = new Date(now);
  
  tomorrow.setDate(today + 1);
  tomorrow.setHours(0, 0, 0, 0);

  return tomorrow;
}

function dateGetLastDayOfWeek(uneDate, dayOfWeek){
  let last = uneDate;
  last.setHours(0, 0, 0, 0);
  while(last.getDay() != dayOfWeek) {
    last.setDate(last - 1)
  }
  return last;
}
function dateGetNextDayOfWeek(uneDate, dayOfWeek){
  let next = uneDate;
  next.setHours(0, 0, 0, 0);
  while(next.getDay() != dayOfWeek) {
    next.setDate(next - 1)
  }
  return next;
}

function dateGetPrevMonday(uneDate){
  // return dateGetLastDayOfWeek(uneDate, 1);
  let lastMonday = uneDate;
  let daysDiff = uneDate.getDay() - 1;
  lastMonday.setDate(uneDate.getDate() - (daysDiff > 0 ? daysDiff : (daysDiff * -6)));
  lastMonday.setHours(0, 0, 0, 0);
  return lastMonday;
}

function customParseFloat(valeur) {
  let retour = valeur;
  if (retour === null) retour = 0;
  if (isNaN(retour)) retour = 0;
  return parseFloat(retour);
}

function convertJourHeure(valeur) {
  let retour = customParseFloat(valeur);
  if (retour < 1.0) {
    retour = (retour/0.125)+'H';
  } else {
    retour = retour+'J';
  }
  return retour;
}

const todayStringDate = formatDatetoStringDate(new Date());
const tomorrowStringDate = formatDatetoStringDate(dateGetTomorrow());