I'm trying to create a new record where one of the fields is a DataTime field.
I always get the "The date-time format for Thu Sep 01 19:25:20 2011 is invalid, or value is outside the supported range." error.
I've tried multiple format options. With Date.parse, Date.parseExact, ... Here 's my last attempt:
//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);
Include the code below and you can just do:
datetime.toCRMFormat();
This code was found in this useful CRM 2011 JS library
// 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;
}
Or if you want them as regular functions instead of extensions on Date such as 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;
}