Internet Explorer new date () return undefined with parameters solution

Source: Internet
Author: User

Problem description

Today, the statistics function is added to the website, one of which isWebsite running time:

This function is enabledCurrent TimeAndSite creation timeAfter calculating the difference, the website running time is calculated as N years, N days, n minutes, n seconds. After the code is written, the browser is tested to display the results. It is found that non-IE browser kernels are normally displayed, when the results are displayed in IE browser, they are displayed as follows: undefined: seconds

After troubleshooting, it is found that new date ("15:16:16") in ie11 returns undefined.

Solve the problem

Method 1: Custom Method

Customize a newdate method:

function NewDate(str){    if(!str){      return 0;    }    arr=str.split(" ");    d=arr[0].split("-");    t=arr[1].split(":");    var date = new Date();     date.setUTCFullYear(d[0], d[1] - 1, d[2]);     date.setUTCHours(t[0], t[1], t[2], 0);     return date;  }  

 

Method 2: Use the date. parse () method

'15:16:16 'cannot be used by browsers to correctly generate date objects using new date (STR. The correct usage is '15:16:16 '.

The date string in '/' format is widely supported by various browsers. The date string connected by '-' can only work in chrome.

var timestart = new Date(Date.parse(‘2017-09-01 15:16:16‘.replace(/-/g,"/")))

Reference: 52484806

Internet Explorer new date () return undefined with parameters solution

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.