2

According to this answer, Firefox and Chrome accepts the format "YYYY MM DD" while creating a date object.

However, Edge doesn't allow new Date("YYYY MM DD") and wants to be initialized as: new Date("YYYY-MM-DD")

So, should i first check which browser is being used before creating a date object or is there a common pattern by which an date object can be created?

Community
  • 1
  • 1
Asqan
  • 4,319
  • 11
  • 61
  • 100

2 Answers2

0

I'm not sure I understant your question because for what I've tried firefox allows you to use new Date("YYYY-MM-DD") and so you could use that and avoid the problem with Edge by using always that, anyway if you're getting an Invalid Date this is my solution by example:

var date;

       date = new Date("10 01 01"); //invalid date
    if(isNaN(date.getDay())){
        date = new Date("2010-01-01")
    }

Hope this helps you

Pizzy
  • 50
  • 9
  • "Date("YYYY-MM-DD")" is not allowed by Firefox. So, i need a common pattern. I understand your avoidance but prefer to have a more clean solution because this function will be called so many times. – Asqan Jan 21 '16 at 15:55
  • I find that very strange because I tried to do so on firefox myself and found this: JavaScript ISO Dates ISO 8601 is the international standard for the representation of dates and times. The ISO 8601 syntax (YYYY-MM-DD) is also the preferred JavaScript date format at http://www.w3schools.com/js/js_date_formats.asp sorry I couldn't be any help – Pizzy Jan 21 '16 at 16:51
0

As mentioned in the previous answer, new Date("YYYY-MM-DD") should work in Firefox. Test this sample code, for example: http://www.w3schools.com/js/tryit.asp?filename=tryjs_date_string_iso1.

There is a separate issue here related to dates that you may have run into, and that is the aligning of UTC dates with user time zones. This thread has more information about this issue and how to accommodate it: Javascript date object always one day off?

Hope this addresses your concerns!

Community
  • 1
  • 1
hxlnt
  • 618
  • 7
  • 7