Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7842325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:17:37+00:00 2026-06-02T16:17:37+00:00

My JavaScript code is working on on Chrome, IE, and Firefox, but when it

  • 0

My JavaScript code is working on on Chrome, IE, and Firefox, but when it gets to safari it doesn’t work. I believe I have narrowed down the problem: When creating a ‘new Date’ in JavaScript, all other browsers recognize my string except safari.

HTML:

<form>
<input id="timeIn" value="1:00 AM"/>
<input id="dateIn" value="1/20/2012"/>
<input id="timeOut" value="2:00 AM"/>
<input id="dateOut" value="1/21/2012"/>
</form>​

JavaScript:

function calcHours() {
    // Build datetime objects and subtract
    var timeIn = new Date($('#timeIn').val() + $('#dateIn').val());
    if(!isValidDate(timeIn)) return -1;
    var timeOut = new Date($('#timeOut').val() + $('#dateOut').val());
    if(!isValidDate(timeOut)) return -1;
    var hours = (timeOut - timeIn)/(1000*60*60);
    return hours.toFixed(1);
}

function isValidDate(d) {
    console.log(d);
    if ( Object.prototype.toString.call(d) === "[object Date]" ) {
      // it is a date
      if ( isNaN( d.getTime() ) ) {  // d.valueOf() could also work
        // date is not valid
          console.log("isn't valid");
          return 0;
      }
      else {
        // date is valid
          console.log("is valid");
          return 1;
      }
    }
    else {
      // not a date
        console.log("isn't valid");
        return 0;
    }
}
var hours = calcHours();
console.log("Hours:", hours);​

I have made a js fiddle: http://jsfiddle.net/mq72k/7/. If you open it up in chrome, it will work fine. If you open it up in Safari, it will say invalid date for the same inputs and not work at all. I have also tried varying the string a bit, adding spaces, different order ect.

Does any one have any ideas how to fix this cross browser compatibility issue?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:17:37+00:00Added an answer on June 2, 2026 at 4:17 pm

    Frankly, I’m surprised other browsers parse the string correctly, as the string you give to the date object is 1:00 AM1/20/2012. Put the date first and add a space in between.

    var timeIn = new Date($('#dateIn').val() + ' ' + $('#timeIn').val());
    var timeOut = new Date($('#dateOut').val() + ' ' + $('#timeOut').val());
    

    http://jsfiddle.net/mq72k/8/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following JavaScript working in IE, but not on Chrome and Firefox.
I have this code and in Firefox is working well, but in Chrome I'am
I have javascript that working fine in Firefox 3.x.x, but it does not work
Hi Javascript gurus, I have this Javascript code which is working fine on Firefox
Following code is working with Firefox browser but onClick is not working with Chrome
Possible Duplicate: Browser Independence issue Code Working for IE but not for Firefox and
This doesn't indicate any error in my javascript code, but in the downloaded jquery.js
I have some troubles with confirm dialog not working with Firefox, but working with
My code (Below) works in Internet Explorer, but not in Chrome, Safari or Mozilla
I have this code working perfectly in firefox. <!DOCTYPE html> <html lang=en> <head> <meta

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.