Results 1 to 2 of 2

Thread: exist any javascript function checks a date if is correct

  1. #1
    Join Date
    Oct 2004
    Posts
    425
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default exist any javascript function checks a date if is correct

    exist any javascript function checks a date if is correct
    eg a wrong date= 31 feb 2009, returns false ?

    To show local time(NOW) GMT+2 to each visitor indepented where he is[time zone] I must use server time zone or exist and js function gives GMT TIME and after I add two hours to this ?
    this is corect
    var today2gmt = Date.UTC()+2*60*60;
    // ms today GMT+2 TIME ANYWHERE

    This builds an object: today2gmt = Date.UTC()+2*60*60 or not I must use
    gmt2 - new Date(today2gmt); // this is corect ?

    To asign to a day/month/year html form fields (date) the current date I must use if FormName="book" and day/month/year field names are "day","month","year" and I have a date object "gmt2"
    document.book.day.value = gmt2.getDay();
    document.book.month.value = months[gmt2.getMonth()]
    document.book.year.value = gmt2.getFullYear();
    when I must call these statements , when page loads at head section ?

  2. #2
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    Quote Originally Posted by leonidassavvides View Post
    exist any javascript function checks a date if is correct
    eg a wrong date= 31 feb 2009, returns false ?
    One does now. When you set an out-of-bounds date, it will "roll over" to the best guess at what you meant.
    Code:
    function dateExists(date, month, year){
        var d = new Date(year, month, date);
        return d.getDate() === parseInt(date); //parseInt makes sure it's an integer.
    }
    //January 1, 2009
    document.write(dateExists(1, 0, 2009) + '<br>'); //true
    //February 31, 2009
    document.write(dateExists(31, 1, 2009) + '<br>'); //false - rolls over to March 3, 2009; 3 !== 31
    Quote Originally Posted by leonidassavvides View Post
    To show local time(NOW) GMT+2 to each visitor indepented where he is[time zone] I must use server time zone or exist and js function gives GMT TIME and after I add two hours to this ?
    this is corect
    var today2gmt = Date.UTC()+2*60*60;
    // ms today GMT+2 TIME ANYWHERE

    This builds an object: today2gmt = Date.UTC()+2*60*60 or not I must use
    gmt2 - new Date(today2gmt); // this is corect ?
    That's incorrect; Date.UTC requires at least two (three in IE) arguments (ECMA, Mozilla, Microsoft). But this works:
    Code:
    var d = new Date();
    d.setHours(d.getUTCHours() + 2);
    (Note that d.getUTCHours will be unreliable after this line, depending on the user's timezone.)

    Quote Originally Posted by leonidassavvides View Post
    To asign to a day/month/year html form fields (date) the current date I must use if FormName="book" and day/month/year field names are "day","month","year" and I have a date object "gmt2"
    document.book.day.value = gmt2.getDay();
    document.book.month.value = months[gmt2.getMonth()]
    document.book.year.value = gmt2.getFullYear();
    when I must call these statements , when page loads at head section ?
    This is a more reliable way:
    Code:
    document.forms.book.day.value = gmt2.getDay();
    document.forms.book.month.value = months[gmt2.getMonth()]
    document.forms.book.year.value = gmt2.getFullYear();
    This must be done either here:
    Code:
    <body>
        Put all your page's contents above the script tag.
        <script type="text/javascript">//Move this script to its own file.
            ...
        </script>
    </body>
    or here:
    Code:
    <head>
        <script type="text/javascript">//Move this script to its own file.
            window.onload = function(){
                ...
            }
        </script>
    </head>
    Last edited by Jesdisciple; 10-28-2008 at 07:13 PM.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

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
  •