This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
FunctionProject/scripts/fonctions_annexes.js

111 lines
3.7 KiB
JavaScript
Raw Normal View History

/* Fonctions Annexes */
// Vérifie si une valeur est vide
function isEmptyValue(value) {
2019-10-13 20:58:13 +02:00
return value === '' || value === null || value === undefined;
}
// Formate les nombres avec des espaces (ex : 76120 = 76 120)
function formatNumberResult(num) {
2019-10-18 20:35:03 +02:00
if(!isNaN(num) && num >= 1000) {
return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1 ');
}
2019-10-18 20:35:03 +02:00
else {
return num;
}
}
2019-10-18 20:35:03 +02:00
// Vérifie si une string est un float (integer exclu)
function isFloat(value) {
2019-10-23 09:33:36 +02:00
return !isNaN(value) && value.toString().includes('.');
2019-10-18 20:35:03 +02:00
}
// Convertit les puissances de 10 en nombre (ex: 1e+20 = 100 000 000 000 000 000 000), ne peut pas dépasser 1e+20 (21 ne fonctionne pas)
function convertPuissanceToNumber(num) {
2019-10-23 09:33:36 +02:00
return formatNumberResult((num).toFixed(0));
}
// Majuscule à la 1ère lettre d'une string
function capitalize (s) {
if (typeof s !== 'string') return ''
return s.charAt(0).toUpperCase() + s.slice(1)
}
// Donne la date et l'heure selon l'UTC (Universal Time Coordinated)
function dateTimeUTC(utc) {
2019-10-04 21:14:48 +02:00
let result = "";
for (let index in utc) {
result = result + utc[index];
}
2019-10-18 09:55:35 +02:00
const enteredOffset = parseFloat(result)*60;
2019-10-04 21:14:48 +02:00
timeNow.setMinutes(timeNow.getMinutes() + enteredOffset);
return showDateTime(enteredOffset);
}
// Affiche la date et l'heure (format : dd/mm/yyyy - 00:00:00)
function showDateTime(enteredOffset) {
2019-10-23 09:33:36 +02:00
const year = timeNow.getFullYear();
const month = ('0'+(timeNow.getMonth()+1)).slice(-2);
const day = ('0'+timeNow.getDate()).slice(-2);
const hour = ('0'+timeNow.getHours()).slice(-2);
const minute = ('0'+timeNow.getMinutes()).slice(-2);
const second = ('0'+timeNow.getSeconds()).slice(-2);
const showDateTimeValue = day + "/" + month + "/" + year + " - " + hour + ":" + minute + ":" + second;
const objectDateTime = {
year: year,
month: month,
day: day,
hour: hour,
minute: minute,
second: second,
showDateTimeValue: showDateTimeValue
};
timeNow.setMinutes(timeNow.getMinutes() - enteredOffset)
2019-10-23 09:33:36 +02:00
return objectDateTime;
}
// Affiche l'heure en temps réel
2019-10-23 09:33:36 +02:00
function realDateTime(id) {
const realDateTimeNow = new Date;
// const year = realDateTimeNow.getFullYear();
// const month = ('0'+(realDateTimeNow.getMonth()+1)).slice(-2);
// const day = ('0'+realDateTimeNow.getDate()).slice(-2);
const hour = ('0'+realDateTimeNow.getHours()).slice(-2);
const minute = ('0'+realDateTimeNow.getMinutes()).slice(-2);
const second = ('0'+realDateTimeNow.getSeconds()).slice(-2);
const resultat = hour + ":" + minute + ":" + second;
document.getElementById(id).innerHTML = resultat;
setTimeout('realDateTime("'+id+'");','1000');
return true;
}
// Vérifie si une date est valide par rapport à la date d'aujourd'hui
2019-10-23 09:33:36 +02:00
function isValidDate(verifyDate, currentDate) {
// Date à vérifier
2019-10-23 09:33:36 +02:00
const toVerifyDate = verifyDate.split('/');
2019-10-18 09:55:35 +02:00
const splitedToVerifyDate = toVerifyDate[2] + '-' + (parseInt(toVerifyDate[1]) + 1) + '-' + toVerifyDate[0];
const msToVerifyDate = Date.parse(splitedToVerifyDate);
// Date courante
currentDate = currentDate.substr(0,10);
2019-10-18 09:55:35 +02:00
const currentDateSplited = currentDate.split('/');
const currentDateFormat = currentDateSplited[2] + '-' + currentDateSplited[1] + '-' + currentDateSplited[0];
const msCurrentDate = Date.parse(currentDateFormat);
if(msToVerifyDate <= msCurrentDate) {
return true;
} else if(msToVerifyDate > msCurrentDate) {
return false;
} else {
return messageError;
}
}
// Créer un cookie de session
function createSessionCookie(name, value) {
document.cookie = escape(name) + "=" + escape(value) + " ; path=/";
}