function round(value,precision) { var exp = Math.pow(10,precision); return Math.round(value*exp)/exp; } function initialize() { var option = document.createElement("option"); option.value = "local"; option.text = "lokalna"; document.chooseForm.timestampTimezone.add(option); for(var zone = -720; zone <= 720; zone+=60) { var option = document.createElement("option"); option.value = -zone; option.text = zone / 60; if(option.value == 0) option.text = "GMT"; if(option.value < 0) option.text = "+" + option.text; document.chooseForm.timestampTimezone.add(option); } document.chooseForm.timestampUnix.value = Math.floor(Date.now() / 1000); document.chooseForm.timestampTimezone.value = "local"; recalculate(document.chooseForm.timestampUnix); } //------tests /* * zwykły pl zima: 1616014892 2021-03-17 22:01:32 loc (+1) * zwykły pl lato: 1592424742 2020-06-17 22:12:22 loc (+2) * strefa 1616015700 2021-03-17 16:15:00 -5 (america/bogota) * strega 1616015700 2021-03-18 05:15:00 +8 (asia/hong kong) */ function recalculate(field) { var form = field.form; if(field.id=="timestampUnix") { var numberRegExp = new RegExp("^[0-9\.,]*$"); if(!numberRegExp.test(field.value)) field.value = field.value.replace(/[^0-9\.,]*/ig,''); var timestamp = field.value; if(form.timestampTimezone.value=="local") { var date = new Date(timestamp * 1000); form.timestampDate.value = date.getFullYear() + "-" + (date.getMonth()+1).toString().padStart(2,"0") + "-" + date.getDate().toString().padStart(2,"0"); form.timestampTime.value = date.getHours().toString().padStart(2,"0") + ":" + date.getMinutes().toString().padStart(2,"0") + ":" + date.getSeconds().toString().padStart(2,"0"); } else { timestamp -= form.timestampTimezone.value * 60; var date = new Date(timestamp * 1000); form.timestampDate.value = date.getUTCFullYear() + "-" + (date.getUTCMonth()+1).toString().padStart(2,"0") + "-" + date.getUTCDate().toString().padStart(2,"0"); form.timestampTime.value = date.getUTCHours().toString().padStart(2,"0") + ":" + date.getUTCMinutes().toString().padStart(2,"0") + ":" + date.getUTCSeconds().toString().padStart(2,"0"); } } if(field.id=="timestampDate") { var dateCharsRegExp = RegExp("^[0-9\-]*$"); if(!dateCharsRegExp.test(field.value)) field.value = field.value.replace(/[^0-9\-]*/ig,''); } if(field.id=="timestampTime") { var dateCharsRegExp = RegExp("^[0-9:]*$"); if(!dateCharsRegExp.test(field.value)) field.value = field.value.replace(/[^0-9:]*/ig,''); } if(field.id=="timestampDate" || field.id=="timestampTime" || field.id=="timestampTimezone") { var dateString = form.timestampDate.value + " " + form.timestampTime.value; if(form.timestampTimezone.value != "local") { if(form.timestampTimezone.value == 0) dateString += " GMT"; else { dateString += " GMT"; if(form.timestampTimezone.value>0) dateString += "-"; else dateString += "+"; dateString += (Math.abs(form.timestampTimezone.value)/60).toString().padStart(2,"0"); dateString += "00"; } } var date = Date.parse(dateString); if(!isNaN(date)) form.timestampUnix.value = Math.floor(date / 1000); } } function selectInput(field) { addClass(field,"input_selected"); for(var i = 0; i < document.forms.length; i++) for(var j = 0; j < document.forms[i].elements.length; j++) { var currentElement = document.forms[i].elements[j]; if(currentElement == field) continue; removeClass(currentElement,"input_selected"); } } function addClass(item, classname) { var obj = item if (typeof item=="string") { obj = document.getElementById(item) } obj.className += " " + classname } function removeClass(item, classname) { var obj = item if (typeof item=="string") { obj = document.getElementById(item) } var classes = ""+obj.className while (classes.indexOf(classname)>-1) { classes = classes.replace (classname, "") } obj.className = classes } Number.prototype.toMoney = function(decimals, decimalSeparator, thousandsSeparator) { var n = this; c = isNaN(decimals) ? 2 : Math.abs(decimals); d = decimalSeparator || '.'; t = (typeof thousandsSeparator === 'undefined') ? ' ' : thousandsSeparator; sign = (n < 0) ? '-' : '', i = parseInt(n = Math.abs(n).toFixed(c)) + '', j = ((j = i.length) > 3) ? j % 3 : 0; return sign + (j ? i.substr(0, j) + t : '') + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : ''); } String.prototype.startsWith = function (str) { return this.indexOf(str) == 0; };