0

In my html document I have a date field:

<input type="date" id="birthDate" class="date-picker">

I get this date in javascript like so:

var birthdate = new Date($('#birthDate').val());

and do some sums:

var today = new Date(Date.now());
var oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
var bornDays = Math.floor(Math.abs((today.getTime() - birthdate.getTime()) / (oneDay)));

The user enters a date in DD/MM/YYYY format on the html page. In Chrome this works fine and gives me the correct number of days since birth 'bornDays'. IE however appears to interpret the entered date as being US format, i.e. MM/DD/YYYY so when I get the date into javascript it thinks the days are months and vice versa. How can I specify that the date is in UK format when getting it from IE?

EXTRA CLARIFICATION:

If the user enters a date 09/11/2018 in the browser

In Chrome, birthdate.getDate() returns 9

In IE,
birthdate.getDate() returns 11

Steve W
  • 1,108
  • 3
  • 13
  • 35

3 Answers3

0

Could you store each part of the data - day, month and year in variables and then construct a string in the order you desire? That should work in all browsers.

var currentTime = new Date();
var gmt = new Date(currentTime.valueOf() + currentTime.getTimezoneOffset() * 60000);
var day = gmt.getDate();
var month = gmt.getMonth() + 1;
var year = gmt.getFullYear();

var dateString = day + '/' + month + '/' + year;
James Howell
  • 1,392
  • 5
  • 24
  • 42
  • No, because getDate() returns the month value when in IE – Steve W Nov 09 '18 at 10:52
  • getDate() does not return the month value, according to https://www.w3schools.com/js/js_date_methods.asp – SPlatten Nov 09 '18 at 10:58
  • What I mean to say is that when a user enters the date in dd/mm/yyyy form in IE, getDate returns the mm value. In other browsers it correctly gets the dd value – Steve W Nov 09 '18 at 11:13
  • Added a GMT conversion. I've tried gmt.getMonth() in my VirtualBox version of IE11 and it returns the correct month for me. – James Howell Nov 09 '18 at 12:05
0

If you know that the string entered will be in DD-MM-YYYY form, parse it and hand its individual components to the Date constructor rather than hoping the Date constructor will be able to guess at the input format.

Or, better yet, provide separate fields for day/month/year (or a calendar widget) so that you're not reliant on users to type a specifically-formatted string.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

To avoid various different interpretations in Date / Time, in various different browsers, it is encouraged to use a librabry such as MomentJS.

Now, let's say you need to get the date out of a string.

var day = moment("1995-12-25");
moment().date(); //will gives you the date

The above will give you the same answer across all browsers.

MomentJS supports following browsers,

Currently the following browsers are used for the ci system: Chrome on Windows XP, IE 8, 9, and 10 on Windows 7, IE 11 on Windows 10, latest Firefox on Linux, and latest Safari on OSX 10.8 and 10.11.

I hope you will find this answer helpful.

Cheers.

Anjana Silva
  • 8,353
  • 4
  • 51
  • 54