简体   繁体   中英

Date Definition in Edge, Chrome and Firefox

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?

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM