oeffisearch/client/js/helpers.js
2020-02-07 14:09:42 +01:00

74 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 convertMinsToHrsMins = (mins) => {
const h = Math.floor(mins / 60);
const m = mins % 60;
if (h > 0) {
return h+'h '+m+'min';
}
return m+'min';
};