Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 941781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:10:10+00:00 2026-05-15T22:10:10+00:00

I am new to JavaScript (but not programming) and am having a difficult time

  • 0

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 [dead link][Guessing of the original code at the bottom]

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.

UPDATE: Code that is probably similar OP’s dead link:

function timestamp(d){
    var year = (d-(Math.round(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);
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:10:11+00:00Added an answer on May 15, 2026 at 10:10 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im new at javascript and i can get this slider to work but not
Not so much a question to help my own programming, but I found this
Ok, so I'm REALLY new to programming and javascript, but I'm having a problem
I am not new to programming, but kind of rusty in Javascript/web development. I
New to javascript, but I'm sure this is easy. Unfortunately, most of the google
I am new to rails, but not to programming or databases. A BETTER PHRASING
Have been following this fabulous tutorial . Being new to Javascript and functional programming,
I'm quite new to javascript programming and got stuck in this problem: I have
I am new to programming in Javascript and this one has clearly 'clean bowled'
I'm quite new to javascript but have undertaken a task to get better aquainted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.