SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Oct 2012
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help in converting years to days,hours,minutes, and seconds.

    I just dont get what to do, I have thought of the process of putting the formulas together. I had thought of creating variables for each unit. But, one question dawns to me is that I dont know what to put as the variable for days if the variable of years would be 365, hours 24, seconds 60. Im at a loss, I am still new to JavaScript and have been searching for 3 hours. I dont know how to put the pieces together in putting it into an equation to post on an HTML. PLEASE HELP!

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by aaksu22 View Post
    I just dont get what to do, I have thought of the process of putting the formulas together. I had thought of creating variables for each unit. But, one question dawns to me is that I dont know what to put as the variable for days if the variable of years would be 365, hours 24, seconds 60. Im at a loss, I am still new to JavaScript and have been searching for 3 hours. I dont know how to put the pieces together in putting it into an equation to post on an HTML. PLEASE HELP!
    Let's start with an assumption, that you are just converting years in to some other unit.

    Code javascript:
    function convertYears(years, unit) {
     
    }

    We can make sure that years is a numeric value, and that unit is one of months, days, hours, minutes, or seconds.

    Code javascript:
    function convertYears(years, unit) {
        years = Number(years) || 0;
        var time;
     
        switch (unit) {
        case 'months':
            // time = ...
            break;
        case 'days':
            // time = ...
            break;
        case 'hours':
            // time = ...
            break;
        case 'minutes':
            // time = ...
            break;
        case 'seconds':
            // time = ...
            break;
        default:
            return;
        }
        return time;
    }

    Now you just need to fill in those commented areas to complete the task.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •