75 lines
1.7 KiB
JavaScript
75 lines
1.7 KiB
JavaScript
'use strict';
|
|
|
|
export const ElementById = (id) => {
|
|
return document.getElementById(id);
|
|
};
|
|
|
|
export const showDiv = (id) => {
|
|
const element = document.getElementById(id);
|
|
if (element) element.classList.remove('hidden');
|
|
};
|
|
|
|
export const hideDiv = (id) => {
|
|
const element = document.getElementById(id);
|
|
if (element) element.classList.add('hidden');
|
|
};
|
|
|
|
export const padZeros = (str) => {
|
|
return ('00' + str).slice(-2);
|
|
};
|
|
|
|
export const isValidDate = (date) => {
|
|
const matches = /^(\d{4})[-\/](\d{2})[-\/](\d{2})$/.exec(date);
|
|
if (matches == null) {
|
|
return false;
|
|
}
|
|
|
|
const d = matches[3];
|
|
const m = matches[2]-1;
|
|
const y = matches[1];
|
|
const composedDate = new Date(y, m, d);
|
|
return composedDate.getDate() == d &&
|
|
composedDate.getMonth() == m &&
|
|
composedDate.getFullYear() == y;
|
|
};
|
|
|
|
export const parseDateTime = (timestamp, format) => {
|
|
const date = new Date(timestamp * 1000);
|
|
|
|
if (format != null) {
|
|
switch (format) {
|
|
case 'full':
|
|
return padZeros(date.getHours()) + ':' + padZeros(date.getMinutes()) + ', ' + date.getDate() + '.' + (date.getMonth() + 1) + '.' + date.getFullYear();
|
|
break;
|
|
|
|
case 'date':
|
|
return date.getDate() + '.' + (date.getMonth() + 1) + '.' + date.getFullYear();
|
|
break;
|
|
|
|
default:
|
|
return false;
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
if (date.toLocaleDateString() !== new Date().toLocaleDateString()) {
|
|
return padZeros(date.getHours()) + ':' + padZeros(date.getMinutes()) + ', ' + date.getDate() + '.' + (date.getMonth() + 1) + '.';
|
|
} else {
|
|
return padZeros(date.getHours()) + ':' + padZeros(date.getMinutes());
|
|
}
|
|
};
|
|
|
|
export const formatDuration = (duration) => {
|
|
const mins = duration / 60;
|
|
const h = Math.floor(mins / 60);
|
|
const m = mins % 60;
|
|
|
|
if (h > 0) {
|
|
return h+'h '+m+'min';
|
|
}
|
|
|
|
return m+'min';
|
|
};
|
|
|