2

我正在尝试创建一个新记录,其中一个字段是 DataTime 字段。

我总是收到“2011 年 9 月 1 日星期四 19:25:20 的日期时间格式无效,或值超出支持范围。” 错误。

我尝试了多种格式选项。使用 Date.parse, Date.parseExact, ... 这是我的最后一次尝试:

//date input format = 2011-02-15 11:05:22

    var year = register_date.substring(0, 4);
    var month = register_date.substring(5, 7);
    var day = register_date.substring(8, 10);
    var hours = register_date.substring(11, 13);
    var minutes = register_date.substring(14, 16);
    var seconds = register_date.substring(17, 19);


    var datetime = Date(Date.UTC(year, month, day, hours, minutes,seconds)); 

    //var datetime = Date.parseExact(register_date, "yyyy-mm-dd HH:mm:ss");

    regFields[4] = new CRMField("ave_subscriptiondate", datetime);
4

1 回答 1

5

包括下面的代码,你可以这样做:

datetime.toCRMFormat();

此代码可在这个有用的 CRM 2011 JS 库中找到

// Formats the date to CRM format
Date.prototype.toCRMFormat = function()
{
    var d = this;
    var f = d.Format("yyyy-mm-ddThh:MM:ss+" + (-d.getTimezoneOffset()/60) + ":00");

    return f;
}

// Formats the date into a certain format
Date.prototype.Format = function(format)
{
   var d = this;
   var f = "";

try {
    f = f + format.replace( /dd|mm|yyyy|MM|hh|ss|ms|APM|\s|\/|\-|,|\./ig , 
    function match()
    {
    switch(arguments[0])
    {
     case "dd": 
        var dd = d.getDate();
        return (dd < 10)? "0" + dd : dd;
     case "mm":
        var mm = d.getMonth() + 1;
        return (mm < 10)? "0" + mm : mm; 
     case "yyyy": return d.getFullYear();
     case "hh": 
        var hh = d.getHours();
        return (hh < 10)? "0" + hh : hh;
     case "MM": 
        var MM = d.getMinutes(); 
        return (MM < 10)? "0" + MM : MM;
     case "ss": 
        var ss = d.getSeconds(); 
        return (ss < 10)? "0" + ss : ss;
     case "ms": return d.getMilliseconds();
     case "APM": 
        var apm = d.getHours(); 
        return (apm < 12)? "AM" : "PM";
     default: return arguments[0];
    }
    });
}
catch(err)
{
}

   return f;
}

或者,如果您希望它们作为常规函数而不是 Date 上的扩展,例如dateToCRMFormat(datetime)

// Formats the date to CRM format
function dateToCRMFormat (date)
{
    return dateFormat(date, "yyyy-mm-ddThh:MM:ss+" + (-date.getTimezoneOffset()/60) + ":00");
}


// Formats the date into a certain format
function dateFormat (date, format)
{
   var f = "";

try {
    f = f + format.replace( /dd|mm|yyyy|MM|hh|ss|ms|APM|\s|\/|\-|,|\./ig , 
    function match()
    {
    switch(arguments[0])
    {
     case "dd": 
        var dd = date.getDate();
        return (dd < 10)? "0" + dd : dd;
     case "mm":
        var mm = date.getMonth() + 1;
        return (mm < 10)? "0" + mm : mm; 
     case "yyyy": return date.getFullYear();
     case "hh": 
        var hh = date.getHours();
        return (hh < 10)? "0" + hh : hh;
     case "MM": 
        var MM = date.getMinutes(); 
        return (MM < 10)? "0" + MM : MM;
     case "ss": 
        var ss = date.getSeconds(); 
        return (ss < 10)? "0" + ss : ss;
     case "ms": return date.getMilliseconds();
     case "APM": 
        var apm = date.getHours(); 
        return (apm < 12)? "AM" : "PM";
     default: return arguments[0];
    }
    });
}
catch(err)
{
}

   return f;
}
于 2011-09-01T18:26:25.687 回答