2
\$\begingroup\$

I made a script in JavaScript to calculate how long it takes until a certain date.
Do you find it user-friendly enough? Is the code correct?

var object = "object"
function formatDate(date) { // get the date in proper format
var d = new Date(date),
    month = '' + (d.getMonth() + 1),
    day = '' + d.getDate(),
    year = d.getFullYear();
    monthT = month;
    yearT = year;
    dayT = day;

if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;

return [year, month, day].join('-');
}

var time = new Object()
time.until = function(date, start){
  if (typeof start === 'undefined'){
  var currentTime = formatDate(new Date())
}else {currentTime == start}
  var year = date.substring(0, 4);
  var year = year - 1 + 1; // now it isn\'t a string anymore :D
  var month = date.substring(5, 7)
  var month = month - 1 + 1;
  var day = date.substring(8, 10);
  var day = day -1 + 1; // make it into a number in stead of string

  var curMonth = monthT
  var curDay = dayT
  var curYear =  yearT

    var curDay = curDay - 1 + 1;
      var curMonth = curMonth - 1 + 1;
      var curYear = curYear - 1 + 1;
    var monthBefor = month - 1;

      var difDay = day - curDay;
      var difMonth = month - curMonth;
      var difWeek = 0;
      if(difDay < 0) {
      difMonth--;
      if(monthBefor === 0){monthBefor = 12};
      switch(monthBefor) {
        case 1:
        case 3:
        case 5:
        case 8:
        case 7:
        case 10:
        case 12:
        difDay = difDay + 31;
        break;
        case 4:
        case 6:
        case 9:
        case 8:
        case 11:
        difDay = difDay + 30;
        break;
        case 2:
        if(year % 4 == 0) {difDay = difDay + 29}else{difDay = difDay + 28}
        break;
        default:
        console.log("error")
        break;

      }
      }
      if(difMonth < 0){
        difMonth += 12;
        year--;
        }
      while(difDay >= 7) {difDay = difDay - 7; difWeek++; }

      var difYear = year - curYear;
return {days: difDay, weeks: difWeek, months: difMonth, years: difYear};

},
time.today = function(object){
    var today = formatDate(new Date);
    if(object == "object"){
        var obj = {};
        var yearO = parseInt(today.substring(0,4))
        var monthO = parseInt(today.substring(5,7))
        var dayO = parseInt(today.substring(8,10))
        var obj = {_when: "today", year: yearO, month: monthO, day: dayO}
        return obj;
    }else{
    return today;
    }
};
time.tomorrow = function(object){
    var tomorrow = formatDate(new Date);
    if(object == "object"){
        var obj = {};
        var yearO = parseInt(tomorrow.substring(0,4))
        var monthO = parseInt(tomorrow.substring(5,7))
        var dayO = parseInt(tomorrow.substring(8,10)) + 1;
        var obj = {_when: "tomorrow", year: yearO, month: monthO, day: dayO}
        return obj;
    }else{

    var tomorrowBegin = tomorrow.substring(0,8)
    var tomorrowEnd = parseInt(dayT) + 1;
     tomorrow = tomorrowBegin + tomorrowEnd
    return tomorrow;
    }
}

Examples:

time.until("2015-09-08") // returns an object with
{days: 6,
months: 1,
weeks: 1,
years: 0}

time.today() // returns the current date in YYYY-MM-DD format
time.tomorrow() // returns the date of the next day in YYYY-MM-DD format

time.today(object) // returns the current date in an object, like
{_when: "today",
day: 26,
month: 7,
year: 2015}
time.tomorrow(object) // returns the date of the next day in an object, like
{_when: "tomorrow",
day: 27,
month: 7,
year: 2015}
\$\endgroup\$
4
  • \$\begingroup\$ It is working! I use it on wsms.ml \$\endgroup\$ Commented Jul 26, 2015 at 17:25
  • \$\begingroup\$ I've rolled back your edit - please see this question on meta which explains what you can and can't do after receiving an answer. \$\endgroup\$
    – RobH
    Commented Jul 27, 2015 at 10:43
  • \$\begingroup\$ @RobH oops sorry \$\endgroup\$ Commented Jul 27, 2015 at 10:51
  • \$\begingroup\$ It's not a problem :) just didn't want you to wonder why I had done it. \$\endgroup\$
    – RobH
    Commented Jul 27, 2015 at 11:12

1 Answer 1

4
\$\begingroup\$

Your indentation is really, really bad. I recommend copying and pasting this code into JSFiddle and then hitting the TidyUp button at the top.

This will go through and add proper indentation to your code. Study from this proper indentation, so you know what to do in the future.


time.until = function (date, start) {
    if (typeof start === 'undefined') {
        var currentTime = formatDate(new Date())
    } else {
        currentTime == start
    }

Why are you bothering with start and currentTime? These parts are absolutely unnecessary and are just plain mean to the person who is trying to read your code and understand why you are using == to set start to currentTime.

Since you don't use currentTime or start anywhere else in that method, just get rid of them.


Avoid creating global variables:

monthT = month;
yearT = year;
dayT = day;

This line is just plain wrong:

var curDay = curDay - 1 + 1;

You are subtracting one from a variable, and then adding one to it? It seems to me that you are turning a string to a number here.

I did a little investigating and traced the variable curDay back to the local day of formatDate. Why are you turning date into a string?

day = '' + d.getDate()

The only place that you are using dayT in the code is when you are initially setting curDay. A few lines later, you are doing that weird - 1 + 1 thing. If you just let date stay as it is and not add '' to turn it into a string, you wouldn't have to do that weird trick.


Not all of the lines that need them have them: semicolons.

Add more to your code.


There is a lot more that needs to be said about your code. I may be able to edit my answer in the future, but I hope more people will add answers to this post.

\$\endgroup\$
2
  • \$\begingroup\$ indentation: fixed, function (date, start) was from something old..: removed, var curDay = curDay - 1 + 1;: replaced with parseInt(), added semicolons But I need the global variables because I use them in another function \$\endgroup\$ Commented Jul 27, 2015 at 10:10
  • \$\begingroup\$ what else is wrong? \$\endgroup\$ Commented Jul 27, 2015 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.