SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member ascensiongraph's Avatar
    Join Date
    Apr 2007
    Location
    Chicago
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Safari Browser issue with Date Object

    I am running a script on a page that tests the current date against the date the user specifies as the date for their entry (Diary Site). If the users entry date is a date greater than the current date they are given an alert that they can only enter a date for the current date or a previous one. Everything operates as it should in IE6 (and above) and Firefox. However, the Safari Browser run on Macs is allowing users to post entries beyond the current date.

    Using alerts to test for the location of the issue, I have found that it is in the
    new Date(); that is created using the values from the form in Safari.

    The line of code giving me the issue is the var formDate = new Date(values): values I assume being the culprit:
    Code:
            <script type="text/javascript">
            <!--
    var formDate = new Date(form.byear.value + " " + form.bmonth.value + " " + form.bday.value);
            //-->
            </script>
    When I run an alert..alert(formDate); I am given the error "Invalid Date".
    In all other browsers I am given:
    Day Month Date Year Hrs:Minutes:Seconds GMT as you would expect.

    I have run tests to ensure that document.byear.value, bmonth and bday are all being passed correctly and they are (In all Browsers including Safari).

    Has anyone else encountered this issue?

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Strings parsed to Dates are browser and language dependent-
    use the core javascript method instead-
    Coerce the inputs to integers, and create the Date as
    new Date(fullyear,month,day):
    And recall that in javascript, months are 0 based-
    January is 0, February is 1, and so on.

    var y=parseInt(form.byear.value);
    var m=parseInt(form.byear.value)-1; // if input is not 0 based
    var d=parseInt(form.bday.value);
    var formDate = new Date(y,m,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
  •