我的要求:
我必须输入“开始日期”和“结束日期”字段,如果保存记录时“结束日期”留空,则“结束日期”字段值将根据输入的起始日期加上 1 年进行填充。
我的问题:
如果开始日期为“9/1/2016”并且结束日期为空,则意味着它应自动将结束日期值填充为“8/31/2016” 但它返回的结束日期值为“9/0/2016”,而且我还收到以下错误消息
错误:JS_EXCEPTION INVALID_FLD_VALUE 您为以下字段输入了无效字段值无效日期:custrecord_end_date
代码: 脚本:客户端脚本,事件:保存记录
function saveRecord(scriptContext) {
var newRecord= scriptContext.currentRecord;
var fromDate = new Date(newRecord.getValue('custrecord_created_date'));
var endDate = newRecord.getValue('custrecord_end_date');
if (endDate == null || endDate == '') {
//getting plus 1 year based on the From Date
tempEndDate = addingPlusYearOfTheCurrentDate(fromDate);
//setting the value to the End Date Field
newRecord.setValue('custrecord_end_date', tempEndDate);
}
}
// Add Plus Year from the Start Date when the End Date is Empty
function addingPlusYearOfTheCurrentDate(fromDate ) {
var date = new Date();
var Month = (fromDate.getMonth() + 1);
var Dates = (fromDate.getDate() - 1);
var Year = (fromDate.getFullYear() + 1);
var last_Day = new Date(Month + '/' + Dates + '/' + Year);
log.debug('last_Day:', last_Day);
return last_Day;
}
最佳答案
不确定为什么您期望能够从 1 中减去 1 并得到除 0 以外的任何值,但您可以通过使用 Date
对象的 setFullYear()
来解决此问题> 和 setDate()
。
function addingPlusYearOfTheCurrentDate(fromDate) {
var date = new Date(fromDate);
date.setFullYear(date.getFullYear() + 1);
date.setDate(date.getDate() - 1);
return date;
}
console.log(addingPlusYearOfTheCurrentDate(new Date(2015, 10, 1)));
关于javascript - 如果当前日期为 1,(getDate() - 1) 函数将获取值 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39361078/