Skip to content

Instantly share code, notes, and snippets.

@M-tower
Last active August 5, 2021 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save M-tower/db8b36a4a8a10fd458b0b98f59abd991 to your computer and use it in GitHub Desktop.
Save M-tower/db8b36a4a8a10fd458b0b98f59abd991 to your computer and use it in GitHub Desktop.
/**
* Converti le string en date JS
* @param {string} d date au format DD/MM/YYYY
* @returns {Date}
*/
function stringToDate(d){
// console.log(d);
let d_ = d.split('/');
// console.log(d + ' ' + (parseInt(d_[1])-1));
return new Date(Date.UTC(d_[2],(parseInt(d_[1])-1),d_[0],0,0,0));
}
/**
* Converti le date JS en string
* @param {Date} d
* @returns {string} au format DD/MM/YYYY
*/
function dateToString(d){
d = new Date(d);
let jr = (d.getDate() >= 10)? d.getDate() : '0'+d.getDate(),
m = ((d.getMonth()+1) >= 10)? (d.getMonth()+1) : '0'+(d.getMonth()+1),
y = d.getFullYear();
return jr + '/' + m + '/' + y;
}
/**
* Converti le date JS en string
* @param {Date} d
* @returns {string} au format lundi 2 août 2021
*/
function dateToStringLong(d){
d = new Date(d);
return new Intl.DateTimeFormat('fr-FR',{weekday: 'short', day: 'numeric', month: 'long', year: 'numeric'}).format(d);
}
/**
* Retourn vrai si d est la date d'aujourd'hui
* @param {Date} d
*/
function isToday(d){
d = new Date(d);
let today = new Date();
if(d.setHours(0,0,0,0) == today.setHours(0,0,0,0)){
return true;
}
return false;
}
/**
* Retourne l'age
* @param {Date} birthday date de naissance en format Date
*/
function calculeAge(birthday) { // birthday is a date
var ageDifMs = Date.now() - birthday.getTime();
var ageDate = new Date(ageDifMs); // miliseconds from epoch
return Math.abs(ageDate.getUTCFullYear() - 1970);
}
/**
* Ajoute des jours (on en retire si value est négatif)
* @param {Date} d élément date de l'opération
* @param {number} value valeur à ajouter
* @returns {Date}
*/
function addDaysToDate(d, value){
return d.setDate(d.getDate() + parseInt(value));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment