I'm working on my way to write most efficient, reusable and robust code, and this is why I'd like to receive some support. I've written a datetimepicker script that allows datetime chosing, and a shortcut button that will subtract from higher date (the right input) a variable number of minutes:

Is there a way to simplify my code even more?

  <input type="text" name="poczatek" id="od" /> 
  <input type="text" name="koniec" id="do" /> 
  <input type="button" value="10" class="odejmijCzas"/> 
  <input type="button" value="20" class="odejmijCzas"/> 
  <input type="button" value="30" class="odejmijCzas"/> 


dateTimeFormat = new Object();
dateTimeFormat.dwucyfrowa = function(m) {
    return m < 10 ? '0' + m : m;
dateTimeFormat.format = function(date) {
    return date.getFullYear() + '-' + dateTimeFormat.dwucyfrowa(date.getMonth() + 1) + '-' + dateTimeFormat.dwucyfrowa(date.getDate()) + ' ' + dateTimeFormat.dwucyfrowa(date.getHours()) + ':' + dateTimeFormat.dwucyfrowa(date.getMinutes());
$(document).ready(function() {
    inputOd = $("#od");
    inputDo = $("#do");

        changeMonth: 'true'

    currentTime = new Date();

    $('.odejmijCzas').click(function() {

function odejmijCzas(liczbaMinut) {
    czasDo = new Date(inputDo.val());
    roznicaCzasu = czasDo.getTime() - liczbaMinut * 1000 * 60;
    czasOd = new Date();
  • \$\begingroup\$ Are you having a problem with your code? \$\endgroup\$ – Andrew Barber May 21 '12 at 7:31
  • \$\begingroup\$ no it's working fine, but i'm wondering if i can shorten it even more \$\endgroup\$ – Malyo May 21 '12 at 7:31
  • \$\begingroup\$ Well this is programming question, i want to know if i can make the code shorter \$\endgroup\$ – Malyo May 21 '12 at 7:32
  • 2
    \$\begingroup\$ one great way to improve it - is not to use Polish for variable names \$\endgroup\$ – zerkms May 21 '12 at 7:33

A quick review:

High Level Overview

This code is okay except for 2 major items: you are not declaring your variables, making them escape all scope. And you use non-English variable names and function names, not every developer speaks Polish, though we should all understand English.


  • English is the one true language for naming, please find English equivalents for odejmijCzas, roznicaCzasu etc.

Idioms and Custom

  • Do not use = new Object();, use = {}


  • You must declare everything with var, let, or const
  • I would declare dateTimeFormat and odejmijCzas inside $(document).ready(function() { to keep the namespace clean


  • Consider using http://jshint.com/ to perfect your code, most of these items were found by this tool as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy