www.webdeveloper.com
Results 1 to 15 of 15

Thread: Does javascript have a simple get day of the year type function?

  1. #1
    Join Date
    Mar 2006
    Posts
    540

    Does javascript have a simple get day of the year type function?

    Hi, wondering if I can get an item by day of the year using javascript. Sort of like their getDay() type commands but for the year instead of the month.
    Don't see anything, does anyone know of anything like this without doing
    a lot of writing to create it? Thank you very much!

  2. #2
    Join Date
    Nov 2002
    Posts
    4,473
    The Date() function in javascript returns the number of milliseconds since 1/1/1970. So you can calculate how many ms since the first of the year and then convert to days:
    Code:
    <script type="text/javascript">
    var today = new Date();
    var first = new Date(today.getFullYear(), 0, 1);
    var theDay = Math.round(((today - first) / 1000 / 60 / 60 / 24) + .5, 0);
    alert("Today is the " + theDay + (theDay == 1 ? "st" : (theDay == 2 ? "nd" : (theDay == 3 ? "rd" : "th"))) + " day of the year");
    </script>

  3. #3
    Join Date
    Mar 2006
    Posts
    540
    Thank you very, very, much, I'll try it and see what happens.

  4. #4
    Join Date
    Nov 2002
    Posts
    4,473
    It looks like from your private message all you want was a day of the week. If so, getDay() will return 0 for Sunday, 1 for Monday, etc.
    Code:
    var daysOfWk = new Array("sunday.htm", "monday.htm", "tuesday.htm", "wednesday.htm", "thursday.htm", "friday.htm", "saturday.htm");
    var today = new Date();
    ...
    document.write('<iframe id="dynstuff" src="' + daysOfWk[today.getDay()] + '"' + iframeprops + '></iframe>');

  5. #5
    Join Date
    Mar 2006
    Posts
    540
    No I was wondering how to add your idea to what I gave you, to make mine appear by day of the year instead. Right now it works just fine doing day of the week or month, I was trying to make it do day of the year instead. Thanks though.

  6. #6
    Join Date
    Mar 2003
    Posts
    575
    I believe that no such function exists.

    And Gil forgot about daylight saving time. His code contains a subtle bug that Rianna might well have not noticed.

    Let me see what I can do:

    Code:
    function dayofyear(d) {   // d is a Date object
    var yn = d.getFullYear();
    var mn = d.getMonth();
    var dn = d.getDate();
    var d1 = new Date(yn,0,1,12,0,0); // noon on Jan. 1
    var d2 = new Date(yn,mn,dn,12,0,0); // noon on input date
    var ddiff = Math.round((d2-d1)/864e5);
    return ddiff+1; }

  7. #7
    Join Date
    Mar 2006
    Posts
    540
    Thank you. Would the day of the year be way off without the daylight savings fixed?

  8. #8
    Join Date
    Mar 2006
    Posts
    540
    Gosh forgot to ask, that code you just gave, will it give me the day of the year? Looks like it will.

  9. #9
    Join Date
    Mar 2003
    Posts
    575
    The usage of my function is as follows:

    Code:
    var a=new Date("July 1, 2007");
    var b=dayofyear(a);
    alert(b);
    If my math is correct, and if the function is correct, this will give you the number 182.

  10. #10
    Join Date
    Mar 2006
    Posts
    540
    Please bear with me Juu, I have only been doing js for 6 months so I just want to make sure I understand it. The two lines you just gave me, is that all I need, or do I need to add that somehow to the rest you gave me. Thanks, Rianna

  11. #11
    Join Date
    Mar 2003
    Posts
    575
    Rianna,

    JavaScript allows you to create your own functions. I strongly suggest you read up on this.

    The first piece of code I gave you was a function I wrote. It contains code which tells your JavaScript interpreter how to calculate the day of the year for a date.

    The second piece of code relies on the first piece of code, and is an example showing how the first piece of code works.

  12. #12
    Join Date
    Mar 2006
    Posts
    540
    Thanks Juu, luckily I know about functions but there was so much code in the first one
    I didn't know if you were sending me ideas, or the real deal. Sounds like the real deal. Thank you very, very, much. I'll see if I can do anything with it. Appreciated, Rianna

  13. #13
    Join Date
    Mar 2006
    Posts
    540
    Juu, you're probably very busy so please only answer me when you have time.

    Any idea how I would call each day of the year to rotate? Without just calling a specific date? Thanks a bunch.

  14. #14
    Join Date
    Mar 2003
    Posts
    575
    Simple. Just grab today's date and stick it in.

    Code:
    var curdate=new Date(); // gets today's date
    var cdnum=dayofyear(curdate);
    document.write('Today is day number '+cdnum+' of the current year.');
    By the way, my code is designed to always count Jan. 1 as day number 1.

  15. #15
    Join Date
    Mar 2006
    Posts
    540
    That is just too cool! Thank you very much, this is what I have been looking for. I'll let you now how it goes. Most of us felines don't pick this stuff up as easily as you fellas seem to, but I guess we're working on it. Thanks again!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles