A couple of date queries

Discussion in 'Javascript' started by Dave Rado, May 13, 2006.

  1. Dave Rado

    Dave Rado Guest

    1) I want to be able to display the date in dd format.

    If in my script file I have:

    var dd = DayOfMonthArray[now.getDate()];

    and in my html page I have:

    <script type="text/javascript">
    document.write(dd);
    </script>

    Then the date displays in d fomat, so instead of 01 I get 1.


    But if in my script file I have:

    var DayOfMonthArray = new Array
    ("01","02","03","04","05","06","07","08",
    "09","10","11","12","13","14","15","16","17","18","19","20","21","22",
    "23","24","25","26","27","28","29","30","31");
    var dd = DayOfMonthArray[now.getDate()];


    and in my html page I have:

    <script type="text/javascript">
    document.write(dd);
    </script>

    Then it adds 1 to the date!! So instead of 01 I get 02! Why? And what's
    the best way of getting round it?


    2) I want to be able to display the year in yy format. now.getYear()
    displays the yeat in yyyy format; what is the best way to get it to
    display in yy format?

    Dave
     
    Dave Rado, May 13, 2006
    #1
    1. Advertising

  2. Dave Rado

    Dave Rado Guest

    Whoops! Line 3 above should have read:

    var dd = now.getDate();
     
    Dave Rado, May 13, 2006
    #2
    1. Advertising

  3. Dave Rado

    Lee Guest

    Dave Rado said:
    >
    >Whoops! Line 3 above should have read:
    >
    >var dd = now.getDate();


    I don't see any line numbers. I don't see anything above.
    If you have a correction to post, just post the corrected code.
    You should be copying your actual code and pasting it into
    your newsreader, to avoid accidents.

    The code:
    var DayOfMonthArray = new Array
    ("01","02","03","04","05","06","07","08",
    "09","10","11","12","13","14","15","16","17","18","19","20","21","22",
    "23","24","25","26","27","28","29","30","31");
    var dd = DayOfMonthArray[now.getDate()];

    will always assign to dd the date of the following day (except on the 31st,
    when you won't get anything. Javascript arrays are indexed beginning at
    zero, not one.

    You could fix your code by adding "00" at the beginning. It would
    never be used, but it would make "01" appear at index position 1.

    On the other hand, you could just use a simple function, instead:

    function zeroPad(n) {
    return (n>10)?n:"0"+n;
    }
    var dd=zeroPad(now.getDate());

    You could use the same funtion with months, being aware that
    the getMonth() method returns a value from 0-11:

    var mm=zeroPad(1+now.getMonth());

    The getYear() method returns 2, 3 or 4 digits, depending on
    the browser and the year in question.
    It's supposed to return the year with 1900 subtracted from it,
    but some browsers have decided that it's better to give the
    users what they expect than what the documentation says they
    should expect.

    var yy=zeroPad(now.getFullYear()%100);

    will always be the two digits you want.


    --
     
    Lee, May 14, 2006
    #3
  4. Dave Rado

    Evertjan. Guest

    Dave Rado wrote on 13 mei 2006 in comp.lang.javascript:

    > var DayOfMonthArray = new Array
    > ("01","02","03","04","05","06","07","08",
    > "09","10","11","12","13","14","15","16","17","18","19","20","21","
    > 22", "23","24","25","26","27","28","29","30","31");
    > var dd = DayOfMonthArray[now.getDate()];
    >


    The first element of an array has number 0,
    so on the 1st the result is 02
    and
    on the 31st the result wil be undefined.

    ========================

    But wy use an array?

    Try for the above:

    var dd = new Date();
    dd = dd.getDate();
    dd = (dd<10)?'0'+dd:''+dd;

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., May 14, 2006
    #4
  5. Dave Rado

    Dave Rado Guest

    Thanks, Lee.

    1) When I use your code in my html page, it works. But when I put your
    code into my script file (with just the "document.wite" statement in my
    html file), I get an "Object not found" error. Any idea what I could
    have done wrong? In my script file I have:

    function zeroPad(n) {
    return (n>10)?n:"0"+n;
    }

    var now = new Date()
    var yy=zeropad(now.getFullYear()%100);

    If instead I have in my scipt file:

    var yy=now.getFullYear()%100;
    var yy = (yy<10)?'0'+yy:''+yy

    ... then it works! I.e. it doesn't seem to like the function when it's
    in the script file, although it's happy with the function when it's on
    the html page instead! Weird.

    2) What does the %100 bit mean?

    3) Given what you said about getYear() returning an unreliable number
    of digits, how can I return a guaranteed four-digit year?


    Dave
     
    Dave Rado, May 14, 2006
    #5
  6. Dave Rado

    Dave Rado Guest

    Thanks, that works well, although I'd like to use Lee's function if I
    could get it to work.

    Dave
     
    Dave Rado, May 14, 2006
    #6
  7. Dave Rado

    Marc Guest

    "Dave Rado" <dave@rado.4dinternet.co.uk> schreef in bericht
    news:...
    > Thanks, Lee.
    >
    > 1) When I use your code in my html page, it works. But when I put your
    > code into my script file (with just the "document.wite" statement in my
    > html file), I get an "Object not found" error. Any idea what I could
    > have done wrong? In my script file I have:
    >
    > function zeroPad(n) {
    > return (n>10)?n:"0"+n;
    > }
    >


    That's probably happening because the page isn't fully loaded yet. Put Lee's
    function back in the script file and document write on page loading
    (completed) like the following:


    <script type="text/javascript">
    window.onload = function(){
    document.write('the stuff here');
    }
    </script>


    > var now = new Date()
    > var yy=zeropad(now.getFullYear()%100);
    >
    > If instead I have in my scipt file:
    >
    > var yy=now.getFullYear()%100;
    > var yy = (yy<10)?'0'+yy:''+yy
    >
    > .. then it works! I.e. it doesn't seem to like the function when it's
    > in the script file, although it's happy with the function when it's on
    > the html page instead! Weird.
    >
    > 2) What does the %100 bit mean?
    >
    > 3) Given what you said about getYear() returning an unreliable number
    > of digits, how can I return a guaranteed four-digit year?
    >
    >
    > Dave
    >
     
    Marc, May 14, 2006
    #7
  8. Dave Rado

    Lee Guest

    Dave Rado said:

    >.. then it works! I.e. it doesn't seem to like the function when it's
    >in the script file, although it's happy with the function when it's on
    >the html page instead! Weird.


    As Marc said, you're using it before it's loaded.


    >2) What does the %100 bit mean?


    http://docs.sun.com/source/816-6408-10/ops.htm#1042403

    "%" is the modulus operator. It returns the remainder
    of the division operation. x%100 evaluates to the two
    right-most digits of x.


    >3) Given what you said about getYear() returning an unreliable number
    >of digits, how can I return a guaranteed four-digit year?


    http://docs.sun.com/source/816-6408-10/date.htm#1193607

    getFullYear() returns the four digit year.


    --
     
    Lee, May 14, 2006
    #8
  9. Dave Rado

    Evertjan. Guest

    Dave Rado wrote on 14 mei 2006 in comp.lang.javascript:

    > Thanks, that works well, although I'd like to use Lee's function if I
    > could get it to work.


    What works wel?

    Please quote what you are replying to.

    If you want to post a followup via groups.google.com, don't use the
    "Reply" link at the bottom of the article. Click on "show options" at the
    top of the article, then click on the "Reply" at the bottom of the article
    headers. <http://www.safalra.com/special/googlegroupsreply/>

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., May 14, 2006
    #9
  10. Lee <> writes:

    > http://docs.sun.com/source/816-6408-10/ops.htm#1042403
    >
    > "%" is the modulus operator. It returns the remainder
    > of the division operation. x%100 evaluates to the two
    > right-most digits of x.


    That's a slightly misleading description of "x%100". The result
    is a number, not a string, so a more correct description
    would be "evalutates to the value of the two right-most digits
    of x (or the digit, if there is only one)".

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, May 14, 2006
    #10
  11. Lasse Reichstein Nielsen wrote:
    > Lee writes:
    >> http://docs.sun.com/source/816-6408-10/ops.htm#1042403
    >>
    >> "%" is the modulus operator. It returns the remainder
    >> of the division operation. x%100 evaluates to the two
    >> right-most digits of x.

    >
    > That's a slightly misleading description of "x%100". The
    > result is a number, not a string, so a more correct
    > description would be "evalutates to the value of the two
    > right-most digits of x (or the digit, if there is only one)".


    And a slightly more correct version may cover the outcome when x is not
    an integer. ;-)

    Richard.
     
    Richard Cornford, May 14, 2006
    #11
  12. Dave Rado

    Dave Rado Guest

    Lasse Reichstein Nielsen wrote:

    > Lee <> writes:
    > > "%" is the modulus operator. It returns the remainder
    > > of the division operation. x%100 evaluates to the two
    > > right-most digits of x.

    >
    > That's a slightly misleading description of "x%100". The result
    > is a number, not a string, so a more correct description
    > would be "evalutates to the value of the two right-most digits
    > of x (or the digit, if there is only one)".


    Odd that the description refers to a division operation, when in this
    instance - now.getFullYear()%100) - there is no division operation!

    Anyway, it's working now. Thanks everyone.

    Dave
     
    Dave Rado, May 14, 2006
    #12
  13. JRS: In article <>,
    dated Sat, 13 May 2006 13:53:15 remote, seen in
    news:comp.lang.javascript, Dave Rado <dave@rado.4dinternet.co.uk> posted
    :
    >1) I want to be able to display the date in dd format.


    But we know what the date is.

    function LZ(x) { return (x>=10||x<0?"":"0") + x }

    D = now.getDate()
    document.writeln("The Date is ", LZ(D), " today.")

    > ...



    >But if in my script file I have:
    >
    >var DayOfMonthArray = new Array
    >("01","02","03","04","05","06","07","08",
    > "09","10","11","12","13","14","15","16","17","18","19","20","21","22",
    > "23","24","25","26","27","28","29","30","31");
    >var dd = DayOfMonthArray[now.getDate()];
    >
    >
    >and in my html page I have:
    >
    ><script type="text/javascript">
    > document.write(dd);
    ></script>
    >
    >Then it adds 1 to the date!!


    No, it does not add 1 to anything; it only gives you a string
    representing a number one larger than you wanted.

    >So instead of 01 I get 02! Why? And what's
    >the best way of getting round it?


    The cause is that arrays start at element 0, not 1. You *could* use an
    initial comma (or "00",)

    var DayOfMonthArray = new Array
    (, "01","02","03","04", ...
    or
    var DayOfMonthArray = [, "01","02","03","04", ...

    Actually, "000102030405...3031".substr(now.getDate()*2, 2) should do;
    but use LZ.


    >2) I want to be able to display the year in yy format. now.getYear()
    >displays the yeat in yyyy format;


    It does not. It returns an integer, the last two decimal digits of
    which are the last two digits of the year, and the first two may
    currently be zero(?), 1, or 20, depending on the browser.

    One must be aware of the type of quantity that a variable may have at
    each stage, especially String/Number.

    > what is the best way to get it to
    >display in yy format?


    ..., LZ(now.getYear()%100), ...

    Or
    now.getFullYear().toString().substring(2)

    but that will fail after about 7993.5 years.

    Read the newsgroup FAQ; see below.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, May 14, 2006
    #13
  14. JRS: In article <>, dated Sat, 13 May 2006
    19:51:37 remote, seen in news:comp.lang.javascript, Lee
    <> posted :
    >
    >The getYear() method returns 2, 3 or 4 digits, depending on
    >the browser and the year in question.


    Not so.

    It returns a Number, an Object holding an IEEE Double. That number
    modulo 100 equals the actual year represented modulo 100 . For the
    years 1999,2000 it may return 99,0 or 99,100 or 99,2001, depending on
    browser.

    ISTM important to be aware of the types of variables and function &
    method results.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, May 14, 2006
    #14
  15. JRS: In article <>,
    dated Sun, 14 May 2006 02:31:47 remote, seen in
    news:comp.lang.javascript, Dave Rado <dave@rado.4dinternet.co.uk> posted
    :

    >1) When I use your code in my html page, it works. But when I put your
    >code into my script file (with just the "document.wite" statement in my

    wite ?
    >html file), I get an "Object not found" error. Any idea what I could
    >have done wrong? In my script file I have:
    >
    >function zeroPad(n) {
    > return (n>10)?n:"0"+n;
    >}
    >
    >var now = new Date()
    >var yy=zeropad(now.getFullYear()%100);
    >
    >If instead I have in my scipt file:

    scipt ?
    >
    >var yy=now.getFullYear()%100;
    >var yy = (yy<10)?'0'+yy:''+yy
    >
    >.. then it works!


    Well, clearly you don't type reliably. Javascript is case-sensitive,
    and so zeropad & zeroPad are entirely different - and in your
    case only one exists.

    >3) Given what you said about getYear() returning an unreliable number
    >of digits, how can I return a guaranteed four-digit year?


    (a) He's wrong; it returns a Number.
    (b) With getFullYear, in browsers not before about MSIE4 level
    (c) With getFY, in js-date0.htm, via FAQ & sig.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, May 14, 2006
    #15
  16. JRS: In article <>, dated Sun, 14 May 2006
    17:55:25 remote, seen in news:comp.lang.javascript, Lasse Reichstein
    Nielsen <> posted :
    >Lee <> writes:
    >
    >> http://docs.sun.com/source/816-6408-10/ops.htm#1042403
    >>
    >> "%" is the modulus operator. It returns the remainder
    >> of the division operation. x%100 evaluates to the two
    >> right-most digits of x.

    >
    >That's a slightly misleading description of "x%100". The result
    >is a number, not a string, so a more correct description
    >would be "evalutates to the value of the two right-most digits
    >of x (or the digit, if there is only one)".


    Well, "decimal" should be there somewhere ... "to the value of all
    digits not to the left of the second digit before the position of the
    decimal point"?

    But I'd prefer a purely arithmetic explanation independent of the value
    of the second operand, as in ECMA-262 11.5.3; though I'd first present a
    version for non-negative integer operands, such as "Generally, X%Y is
    the remainder on dividing the non-negative integer X by the positive
    integer Y; in particular languages the implementation is extended to
    negative and/or negative operands in more than one way."


    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
    Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links;
    Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
    No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
     
    Dr John Stockton, May 14, 2006
    #16
  17. JRS: In article <>
    , dated Sun, 14 May 2006 09:50:59 remote, seen in
    news:comp.lang.javascript, Dave Rado <dave@rado.4dinternet.co.uk> posted
    :
    >
    >Odd that the description refers to a division operation, when in this
    >instance - now.getFullYear()%100) - there is no division operation!


    The value of getFullYear is _divided_ into two parts; the first, which
    is the largest multiple of 100 that fits in Year, is dropped, and the
    result is the remainder.

    In practice, % will be implemented by a hardware division.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, May 14, 2006
    #17
  18. Dave Rado wrote:

    > Lasse Reichstein Nielsen wrote:
    >> Lee <> writes:
    >> > "%" is the modulus operator. It returns the remainder
    >> > of the division operation. x%100 evaluates to the two
    >> > right-most digits of x.

    >>
    >> That's a slightly misleading description of "x%100". The result
    >> is a number, not a string, so a more correct description
    >> would be "evalutates to the value of the two right-most digits
    >> of x (or the digit, if there is only one)".

    >
    > Odd that the description refers to a division operation, when in this
    > instance - now.getFullYear()%100) - there is no division operation!


    Yes, there is. Internally. Or do you think the machine simply /guesses/
    the remainder? :)


    PointedEars
    --
    But he had not that supreme gift of the artist, the knowledge of
    when to stop.
    -- Sherlock Holmes in Sir Arthur Conan Doyle's
    "The Adventure of the Norwood Builder"
     
    Thomas 'PointedEars' Lahn, May 22, 2006
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,302
    Michael Borgwardt
    May 30, 2004
  2. Mantorok

    Couple of queries...

    Mantorok, May 11, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    334
    Mantorok
    May 11, 2006
  3. Ian Roddis

    xslt queries in xml to SQL queries

    Ian Roddis, Feb 26, 2006, in forum: Python
    Replies:
    3
    Views:
    1,533
    Crutcher
    Feb 26, 2006
  4. Marco Alting

    Dependent queries in Access and Entry Date

    Marco Alting, Sep 4, 2003, in forum: ASP General
    Replies:
    0
    Views:
    107
    Marco Alting
    Sep 4, 2003
  5. Abby Lee

    so many queries within queries I'm confused

    Abby Lee, Aug 4, 2004, in forum: ASP General
    Replies:
    11
    Views:
    367
    Aaron [SQL Server MVP]
    Aug 6, 2004
Loading...

Share This Page