tags:

views:

74

answers:

5

I am new to JavaScript (but not programming) and am having a difficult time figuring out where I made a mistake in this function, found here: http://mikeryan.webatu.com/function.html

The function should take a DDMMYY timestamp and convert it into a human-readable string. For instance, 210710 would be turned into July 21st, 2010. I've looked over it about a hundred times, but unfortunately exhaustion is getting the better of me. Any help would be extremely appreciated!

+4  A: 

One problem: you're missing a parenthesis. Change:

var year = (d-(Math.round(d / 100)*100);

to

var year = (d-(Math.round(d / 100)*100));

That being said, this is a more straightforward calculation method:

var year = d % 100;
var month = Math.floor(d / 100) % 100;
var day = Math.floor(d / 10000) % 100;

Next, your array initialization is unnecessarily verbose. Instead of:

var arr = new Array();
arr[0] = "foo";
arr[1] = "bar";

just do:

var arr = ["foo", "bar"];

Your day suffix is incorrect. It puts "nd" after 12 and "12nd April" clearly isn't correct. I would just use logic for doing this rather than a lookup array where most elements are "th".

So:

function timestamp(d){
  var year = d % 100;
  var month = Math.floor(d / 100) % 100;
  var day = Math.floor(d / 10000) % 100;
  var months = ["January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"];
  if (year>20) {
    year = '19' + year;
  } else {
    year = '20' + year;
  }
  if (day == 1 || day == 21 || day == 31) {
    var suffix = "st";
  } else if (day == 2 || day == 22) {
    var suffix = "nd";
  } else {
    var suffix = "th";
  }
  return (months[month-1] + ' ' + day + suffix + ', ' + year);
}

Lastly there is little value in your "timestamp" being an integer in its present form. A more typical format for tis kind of thing is YYYYMMDD for two reasons:

  1. Numerical ordering matches date ordering; and

  2. It's unambiguous. North Americans put month before day (ie MMDDYY). Everyone else in the world puts day first (ie DDMMYY). No one does YYDDMM.

cletus
Yup, I'm tired and totally missed that one. Thanks a lot!
Mike R
Thanks, cletus! Your edited reply was extremely helpful, and a great learning experience for me.
Mike R
The only reason I had chosen to go with an integer in the first place was to do time comparisons in a later function, to determine the amount of time between two dates. As for the second point, I realized that mistake earlier and have changed it on the server. Again, thanks for the help!
Mike R
A: 

You are missing a parenthesis on the first line of the function.

JGB146
Thanks! Missed this one.
Mike R
A: 

I'd use % - modulo: X modulo 100 discards anything except the last 2 digits. Useful!

also use floor not round

Sanjay Manohar
Alright, thanks for the tip. Will make the changes.
Mike R
A: 

Use the date object. It's a whole lot faster. Here is a quick example it assumes the year is going to be in the 2000s so you would have to do some modifications. And the output is not exactly what you have, but it is pretty close and the code is a whole lot shorter.

function date(e){
  var d = new Date();
  d.setYear(2000+e.substring(4)/1,e.substring(2,4)-1,e.substring(0,2)-1);
  alert(d.toDateString());
}
qw3n
A: 

First use Math.floor to get the floor value of the decimal, then there were some typos in your function. Here is the code that works (Note: just tested with a couple of examples) but should be enough to get your started:

          function timestamp(d){
        var year = (d-(Math.floor(d / 100)*100));

        var day = Math.floor(d/10000);
        var dayfix = (day - (Math.floor(day/10)*10));

        // var month = ((d-year)-(day*100000)/100);

        var a = (d - year);
        var b = ((day * 100000) / 10);

        var month = (a - b) / 100;

        var months = new Array();
        months[1]  = "January";
        months[2]  = "February";
        months[3]  = "March";
        months[4]  = "April";
        months[5]  = "May";
        months[6]  = "June";
        months[7]  = "July";
        months[8]  = "August";
        months[9]  = "September";
        months[10]  = "October";
        months[11] = "November";
        months[12] = "December";

        var daysuffix = new Array();
        daysuffix[0] = "th";
        daysuffix[1] = "st";
        daysuffix[2] = "nd";
        daysuffix[3] = "rd";
        daysuffix[4] = "th";
        daysuffix[5] = "th";
        daysuffix[6] = "th";
        daysuffix[7] = "th";
        daysuffix[8] = "th";
        daysuffix[9] = "th";

        if(year>20){
           year = '19' + year;
        }
        else{
           year = '20' + year;
        }
        return (months[month] + ' ' + day + daysuffix[dayfix] + ', ' + year);
     }
naikus