Skip to content Skip to sidebar Skip to footer

New Date("yyyy/mm") Not Work On Ie 11

In my project, i use js code below to take date with date input only year and month: var current_time = new Date(current); with current data is something like this: '2017/04' It

Solution 1:

Dates should be formatted according RFC 2822 or ISO 8601 So if you use '-' instead of '/ it will work every where.

console.log(newDate("2017-04"))

if you want to still have your date with '/' you can do this

console.log(newDate("2017/04".replace(/\//g, "-")));

Solution 2:

The format you are using is not a standard format of date. Non standard date formats cause problems on various browsers like IE, safari, etc.

Use this method. It will work on all IE as well

Run it on chrome and IE. Here in this snippet, it will give one month less since stack snippet is using a different library for date parsing.

var input = "2017/04"var year = input.split("/")[0]

// indexing starts with 0var month = parseInt(input.split("/")[1],10) -1var date = newDate(year,month)
console.log(date)

This is what it will output on browsers including IE

Sat Apr 01201700:00:00 GMT+0530 (India Standard Time)

Solution 3:

In order to create the date by passing the month and year to dateObject you can do:

current = '2017/04';
current = current.split("/")
var date = newDate(current[0], current[1]);
// it will return the date object where date is set as the first day of the monthconsole.log(date)

which will give you the date object set to the first day of the month.

If you want to get the current month and year you can do:

var year = newDate().getFullYear();
var month = newDate().getMonth() + 1;
date = year + '/' + month;
console.log(date);

Post a Comment for "New Date("yyyy/mm") Not Work On Ie 11"