Files
BeWoPlaner/BeWoPlanerMobil/Scripts/dateHelper.js

253 lines
6.8 KiB
JavaScript

function getDateTimeStringWithLeadingZeros(pDate) {
const dateStr = getDateStringWithLeadingZeros(pDate);
const hh = (pDate.getHours() < 10 ? "0" : "") + pDate.getHours();
const m = (pDate.getMinutes() < 10 ? "0" : "") + pDate.getMinutes();
const ss = (pDate.getSeconds() < 10 ? "0" : "") + pDate.getSeconds();
return dateStr + " " + hh + ":" + m + ":" + ss;
}
function getDateStringWithLeadingZeros(pDate) {
const dd = (pDate.getDate() < 10 ? "0" : "") + pDate.getDate().toString();
const mm = ((pDate.getMonth() + 1) < 10 ? "0" : "") + (pDate.getMonth() + 1);
const yyyy = pDate.getFullYear();
return dd + "." + mm + "." + yyyy;
}
function subtractDates(date1, date2) {
const t1 = date1.getTime();
const t2 = date2.getTime();
const diff = (t2 - t1) / 60000;
return Math.abs(diff);
}
function addMinutes(date, minutes) {
return new Date(date.getTime() + minutes * 60000);
}
function stringToTime(str) {
const defaultResult = [-1, -1];
str = str.replace(":", "");
str = str.replace(".", "");
str = str.replace(" ", "");
if(str.length < 3) {
if(str.length < 2) {
str = `0${str}`;
}
while(str.length < 4) {
str = str + "0";
}
} else {
while(str.length < 4) {
str = `0${str}`;
}
}
const minutes = str.slice(2, 4);
const hours = str.slice(0, 2);
defaultResult[0] = parseInt(hours);
defaultResult[1] = parseInt(minutes);
return defaultResult;
}
function actualDurationWithEndDate() {
if($("#enddatum_textbox").val() !== "") {
if($("#enddatum_textbox").val() !== $("#datum_textbox").val()) {
const endDate = getEndDateObj().getDate();
const startDate = getDateObj(true).getDate();
const diffInMs = getEndDateObj() - getDateObj(true);
const diffInS = diffInMs / 1000;
const diffInMins = diffInS / 60;
console.log(`end date: ${endDate}`);
console.log(`difference in minutes: ${diffInMins}`);
const dateDifference = endDate - startDate;
const endDateString = $("#ende_textbox").val();
const startDateString = $("#start_textbox").val();
let duration = endDateString - startDateString;
console.log(`dateDifference: ${dateDifference}`);
const rest = duration % 100;
duration = duration - rest;
duration = duration / 100 * 60;
duration = dateDifference * 24 * 60 + duration + rest;
$("#duration_textbox").val(duration).trigger("change");
}
}
}
function getEndDateObj() {
const roh = $("#enddatum_textbox").val();
console.log(`${roh}`);
const day = parseInt(roh.slice(0, 2));
const month = parseInt(roh.slice(3, 5));
const year = parseInt(roh.slice(6, 10));
var hoursAndMinutes = [0, 0];
var date = new Date();
if(!isNaN(day) && !isNaN(month) && !isNaN(year)) {
date = new Date(year, month - 1, day);
}
const endDateString = $("#ende_textbox").val();
if(endDateString !== "") {
hoursAndMinutes = stringToTime(endDateString);
}
date.setHours(hoursAndMinutes[0]);
date.setMinutes(hoursAndMinutes[1]);
return date;
}
function getHoursAndMinutesWithLeadingZeros(pDate) {
const hours = pDate.getHours();
const minutes = pDate.getMinutes();
const hoursString = hours < 10 ? `0${hours.toString()}` : hours.toString();
const minutesString = minutes < 10 ? `0${minutes.toString()}` : minutes.toString();
return hoursString + ":" + minutesString;
}
function toShortDateString(dateString, trennzeichen) {
const geteilt = dateString.split(trennzeichen);
var yyyy = "";
const mm = geteilt[1];
const dd = geteilt[0];
for(let i = 0; i < 4 - geteilt[2].split(" ")[0].length; i++) {
yyyy += "0";
}
yyyy += geteilt[2].split(" ")[0];
return (dd < 10 ? "0" : "") + dd + "." + (mm < 10 ? "0" : "") + mm + "." + yyyy;
}
function toDateStringYearMonthDay(pDate) {
const dd2 = pDate.getDate();
const mm2 = pDate.getMonth() + 1;
const yyyy2 = pDate.getFullYear();
const ddStr2 = dd2 < 10 ? "0" + dd2 : dd2;
const mmStr2 = mm2 < 10 ? "0" + mm2 : mm2;
return yyyy2 + "-" + mmStr2 + "-" + ddStr2;
}
function checkEndDate() {
$.ajax({
type: "GET",
url: CheckEndDateUrl,
success: function(json) {
const endDateSettingsString = $.parseJSON(json);
const endDateSetting = parseInt(endDateSettingsString);
if(endDateSetting === 1) {
$("#EndDate_div").css("display", "block");
$("#enddatum_textbox").val($("#datum_textbox").val());
}
else {
$("#EndDate_div").css("display", "none");
}
}
});
}
function calcDurationByStartEnd() {
if($("#enddatum_textbox").val() === $("#datum_textbox").val() || $("#enddatum_textbox").val() === "") {
const shm = getDateObj(true);
const ehm = getDateObj(false);
let dur = 0;
if(shm.getTime() < ehm.getTime()) {
dur = parseInt(subtractDates(ehm, shm));
}
$("#duration_textbox").val(dur).trigger("change");
}
else {
actualDurationWithEndDate();
}
}
function calcEndByStartDuration() {
if($("#enddatum_textbox").val() === $("#datum_textbox").val() || $("#enddatum_textbox").val() === "") {
const shm2 = getDateObj(true);
const dauer = parseInt($("#duration_textbox").val());
if(isNaN(dauer)) {
return;
}
const neu = addMinutes(shm2, dauer);
const ende = (neu.getHours() < 10 ? "0" : "") + neu.getHours() + ":" + (neu.getMinutes() < 10 ? "0" : "") + neu.getMinutes();
$("#ende_textbox").val(ende).trigger("change");
}
else {
actualDurationWithEndDate();
}
}
function calcStartByEndDuration() {
const ehm2 = getDateObj(false);
const dauer2 = parseInt($("#duration_textbox").val());
if(isNaN(dauer2)) {
return;
}
const neu2 = addMinutes(ehm2, dauer2 * -1);
$("#start_textbox").val((neu2.getHours() < 10 ? "0" : "") + neu2.getHours() + (neu2.getMinutes() < 10 ? "0" : "") + neu2.getMinutes()).trigger("change");
}
function getDateObj(isFromStart) {
const roh = $("#datum_textbox").val();
const dd = parseInt(roh.slice(0, 2));
const mm = parseInt(roh.slice(3, 5));
const yyyy = parseInt(roh.slice(6, 10));
var date = new Date();
if(!isNaN(dd) && !isNaN(mm) && !isNaN(yyyy)) {
date = new Date(yyyy, mm - 1, dd);
}
var hm = [0, 0];
var ds = $("#ende_textbox").val();
if(isFromStart) {
ds = $("#start_textbox").val();
}
if(ds !== "") {
hm = stringToTime(ds);
}
date.setHours(hm[0]);
date.setMinutes(hm[1]);
return date;
}