SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    if ($zee == "Guru") { $zee--;}
    Join Date
    Nov 2005
    Location
    Karachi - Pakistan
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Convert days to weeks and days

    Hello

    I need some help about converting days to weeks and remaining days.

    For example :

    Total days : 152 = 147 Weeks and 5 Days:

    so I want to write a function getWeeks(152) and it will return an array of 3 elements 0 will be weeks and 1 will be days.

    Please help
    Zeeshan

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,840
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You didn't say what you wanted returned in the third entry in the array but here's the function that returns weeks in element zero and left over days in element one.

    Code:
    function getWeeks(d) {
    a[0] = Math.floor(d/7);
    a[1] = d%7;
    return a;
    }
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePοint Troll disgracian's Avatar
    Join Date
    Aug 2006
    Location
    Samsara
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It might be more meaningful to return a basic object instead.
    Code:
    function getWeeks(days) {
      return {
        weeks : Math.floor(days / 7),
        days : days &#37; 7
      };
    }
    Then you can use it like so:
    Code:
    var obj = getWeeks(152);
    alert(obj.weeks); // alerts '21'
    alert(obj.days); // alerts '5'
    Using objects is usually a bit nicer, and the intent of the values is more obvious, especially in more complex objects and when it's been six months or so since you last looked at the code. 'obj.weeks' is a lot more meaningful at a glance than 'obj[0]'.

    Cheers,
    D.

  4. #4
    if ($zee == "Guru") { $zee--;}
    Join Date
    Nov 2005
    Location
    Karachi - Pakistan
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow !

    Thanks a lot !

    @felgall
    I dont have words for you. All I can say is that Javascript Guru is a very small badge for you. For me, you are JAVASCRIPT !

    @disgracian
    Thanks for your feed back !

  5. #5
    SitePοint Troll disgracian's Avatar
    Join Date
    Aug 2006
    Location
    Samsara
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by zeeshanhashmi View Post
    For me, you are JAVASCRIPT !
    If that were true, we could add bits to him by calling his prototype object. I don't know if he'd like that.

    Cheers,
    D.


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
  •