dry-node
Version:
Initialiseur de structure Node Express typée et modulaire
248 lines (201 loc) • 6.53 kB
JavaScript
class DryHelperHandlebar {
/**
* return ex. vendredi 3 Juin 21
* @param date
*/
getDateFormatted1(date) {
const d = new Date(date);
// const day = this.getDayOfWeekFr(d.getDay()).substr(0,3);
const day = this.getDayOfWeekFr(d.getDay());
const jour = this.completeNumberTwoCaractere(d.getDate());
const month = this.completeNumberTwoCaractere(d.getMonth() + 1);
return jour + "-" + month + "-" + d.getFullYear();
}
/**
* return ex. vendredi 3 Juin 21
* @param date
*/
getDateFormattedFrench(date) {
const d = new Date(date);
// const day = this.getDayOfWeekFr(d.getDay()).substr(0,3);
const day = this.getDayOfWeekFr(d.getDay());
const jour = this.completeNumberTwoCaractere(d.getDate());
const month = this.getMonthLetterFr(this.completeNumberTwoCaractere(d.getMonth() + 1));
return day + " " + jour + " " + month + " " + d.getFullYear();
}
/**
* return ex. vendredi 3 Juin 21
* @param date
*/
getDateTimeFormattedFrench(date) {
const d = new Date(date);
// const day = this.getDayOfWeekFr(d.getDay()).substr(0,3);
const day = this.getDayOfWeekFr(d.getDay());
const jour = this.completeNumberTwoCaractere(d.getDate());
const month = this.getMonthLetterFr(this.completeNumberTwoCaractere(d.getMonth() + 1));
const hour = this.completeNumberTwoCaractere(d.getHours())
const minute = this.completeNumberTwoCaractere(d.getMinutes())
return day + " " + jour + " " + month + " " + d.getFullYear() + " à " + hour + ":" + minute;
}
estimeTime(estimateTime){
return new DryHelperHandlebar().getDuration(estimateTime);
}
operation(ops, val1, val2){
if(ops === '-'){
return val1 - val2
}
return 0
}
/**
* Get Day of week in string in french
* @return string
* @param dayId
*/
getDayOfWeekFr(dayId) {
switch (dayId) {
case 0 :
return 'Dimanche';
case 1 :
return 'Lundi';
case 2 :
return 'Mardi';
case 3 :
return 'Mercredi';
case 4 :
return 'Jeudi';
case 5 :
return 'Vendredi';
case 6 :
return 'Samedi';
default:
return '';
}
}
/**
* Complete 0 for obtain two number
* @param n
* @return string
*/
completeNumberTwoCaractere(n) {
if (n <= 9) {
return '0' + n.toString();
}
return n.toString();
}
/**
* Get Month letter
* @param m
* @return string
*/
getMonthLetterFr(m) {
switch (m) {
case '01':
return 'Janvier';
case '02':
return 'Fevrier';
case '03':
return 'Mars';
case '04':
return 'Avril';
case '05':
return 'Mai';
case '06':
return 'Juin';
case '07':
return 'Juillet';
case '08':
return 'Août';
case '09':
return 'Septembre';
case '10':
return 'Octobre';
case '11':
return 'Novembre';
case '12':
return 'Decembre';
default :
return '';
}
}
/**
* get duration by second
* @param second
*/
getDuration(second) {
// get seconds (Original had 'round' which incorrectly counts 0:28, 0:29, 1:30 ... 1:59, 1:0)
var seconds = Math.round(second % 60);
// remove seconds from the date
second = Math.floor(second / 60);
// get minutes
var minutes = Math.round(second % 60);
// remove minutes from the date
second = Math.floor(second / 60);
// get hours
var hours = Math.round(second % 24);
// remove hours from the date
second = Math.floor(second / 24);
// the rest of timeDiff is number of days
var days = second;
if (days > 0) {
return {
label: days + 'jr ' + hours + 'h',
status: 'success'
};
} else {
if (hours > 0) {
return {
label: hours + 'h' + minutes + '\' ',
status: 'warning'
};
} else {
return {
label: minutes + 'min ',
status: 'warning'
};
}
}
}
/**
* difference before to date
* @param dateOne
* @param dateSecond
*/
differenceBetween2Date(dateOne, dateSecond) {
const diffTime = Math.abs(dateOne - dateSecond);
// strip the ms
var timeDiff = diffTime / 1000;
// get seconds (Original had 'round' which incorrectly counts 0:28, 0:29, 1:30 ... 1:59, 1:0)
var seconds = Math.round(timeDiff % 60);
// remove seconds from the date
timeDiff = Math.floor(timeDiff / 60);
// get minutes
var minutes = Math.round(timeDiff % 60);
// remove minutes from the date
timeDiff = Math.floor(timeDiff / 60);
// get hours
var hours = Math.round(timeDiff % 24);
// remove hours from the date
timeDiff = Math.floor(timeDiff / 24);
// the rest of timeDiff is number of days
var days = timeDiff;
if (days > 0) {
return {
label: days + "jr " + hours + "h",
status: 'success'
}
} else {
if (hours > 0) {
return {
label: hours + "h" + minutes + "' ",
status: 'warning'
}
} else {
return {
label: minutes + "min ",
status: 'warning'
}
}
}
}
}
module.exports = DryHelperHandlebar;