0

The function below calculates the age based of the given parameters.

var now = new Date();
var past = new Date(select_year + "-" + select_month + "-" + select_day); 
var nowYear = now.getFullYear();
var pastYear = past.getFullYear();
var age = nowYear - pastYear;

It works perfectly in Chrome.

But when I switch over to Safari, the following line says Invalid Date:

var past = new Date(select_year + "-" + select_month + "-" + select_day);

I'm assuming it is failing to reconstruct the date.


Is there a way to fix this?

Nikk
  • 7,384
  • 8
  • 44
  • 90
  • 1
    Possible duplicate of [Javascript Date() function not working in Safari (iOS)](https://stackoverflow.com/questions/29652102/javascript-date-function-not-working-in-safari-ios) – CyanCoding Oct 15 '17 at 01:30

1 Answers1

4

For Internet Explorer and Safari, you need to change the format up a bit. I believe that this will work for you:

var d = new Date(2011, 01, 07);  
var d = new Date(2011, 01, 07, 11, 05, 00); 
var d = new Date("02/07/2011");  
var d = new Date("02/07/2011 11:05:00");  
var d = new Date(1297076700000); 
var d = new Date("Mon Feb 07 2011 11:05:00 GMT"); 

Found here.

CyanCoding
  • 1,012
  • 1
  • 12
  • 35