字符串不会转换为日期

时间:2017-02-07 15:08:02

标签: javascript date

这里遇到了一个让我发疯的问题。 2个变量被实例化为日期,一个带有字符串,另一个带有硬编码值。如果查看调试窗口,mString的值与硬编码值完全相同,但它会产生无效日期。 不明白为什么,但我肯定会欣赏任何指针。 我正在使用IE 11.它适用于所有其他浏览器

mString的创建方式如下: -

mString = dateParts[2] + "-" + mon.toString() + "-" + dateParts[1]

控制台中的错误与此

无关

Debug Window

2 个答案:

答案 0 :(得分:0)

这似乎有效,我已经在IE11和Chrome中测试了以下内容,两个警报都适合你吗?

<?xml version="1.0" encoding="utf-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">
<servlet>
    <servlet-name>EndpointsServlet</servlet-name>
    <servlet-class>com.google.api.server.spi.EndpointsServlet</servlet-class>
    <init-param>
        <param-name>services</param-name>
        <param-value>com.somename.project.endpoints.MyEndpoint</param-value>
    </init-param>
    <init-param>
        <param-name>restricted</param-name>
        <param-value>false</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>EndpointsServlet</servlet-name>
    <url-pattern>/_ah/api/*</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

<filter>
    <filter-name>endpoints-api-configuration</filter-name>
    <filter-class>com.google.api.control.ServiceManagementConfigFilter</filter-class>
</filter>
<filter>
    <filter-name>endpoints-api-controller</filter-name>
    <filter-class>com.google.api.control.extensions.appengine.GoogleAppEngineControlFilter</filter-class>
    <init-param>
        <param-name>endpoints.projectId</param-name>
        <param-value>myproject</param-value>
    </init-param>
    <init-param>
        <param-name>endpoints.serviceName</param-name>
        <param-value>myproject.appspot.com</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>endpoints-api-configuration</filter-name>
    <servlet-name>EndpointsServlet</servlet-name>
</filter-mapping>
<filter-mapping>
    <filter-name>endpoints-api-controller</filter-name>
    <servlet-name>EndpointsServlet</servlet-name>
</filter-mapping>

答案 1 :(得分:0)

使用此:

var pattern = /(\d{4})-(\d{2})-(\d{2})/;
var date = new Date(mString.replace(pattern,'$1-$2-$3'));