(getDate() - 1) 如果当前日期为 1,函数将获取值零

(getDate() - 1) function is getting the value zero if the current date is 1

我的要求:

我必须填写“开始日期”和“结束日期”字段,如果在保存记录时“结束日期”留空,则“结束日期”字段值会根据输入的开始日期加上 1 年。

我的问题:

如果开始日期是“9/1/2016”并且结束日期留空意味着它应该自动将结束日期值填充为“ 8/31/2016" 但它 将结束日期值返回为 "9/0/2016" 而且我还收到以下 错误留言

错误:JS_EXCEPTION INVALID_FLD_VALUE 您为以下字段输入了无效字段值无效日期:custrecord_end_date

代码: 脚本:客户端脚本,事件:SaveRecord

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)));

使用 Date 构造函数(和 Date.parse 解析字符串,它们等效于解析)强烈 反对,因为解析几乎完全依赖于实现且不一致。使用自定义函数手动解析字符串或使用库。

向日期添加年份非常简单,但您似乎想要明年同一日期前一天的日期。所以加一年减一天。

// Parse m/d/y format string to a Date and validate the result
function parseMDY(s) {
  var b = s.split(/\D/);
  var d = new Date(b[2], --b[0], b[1]);
  return d && d.getMonth() == b[0]? d : new Date(NaN);
}
   
// Add 1 year to a Date
function addYear(d) {
  if (Object.prototype.toString.call(d) != '[object Date]') return;
  d.setFullYear(d.getFullYear() + 1);
  d.setDate(d.getDate() -1);
  return d;
}

var d = parseMDY('9/1/2016');
console.log(d.toLocaleString())
addYear(d);
console.log(d.toLocaleString())

请注意,对于 2 月 29 日,加上一年得到 5 月 1 日,然后减去一天得到 2 月 28 日。

你应该使用方法 nlapiStringToDate() 进行字符串到日期的转换,因为 NetSuite 将日期字段值作为字符串,你必须将其转换为日期,并且在你设置返回日期之前,你必须使用 nlapiSetFieldValue(YOUR_FIELD_ID, nlapiStringToDate(dateObject))

请参阅下面有关读取和设置日期字段的建议用法。

function saveRecord(scriptContext) {
  var newRecord = scriptContext.currentRecord;
  var fromDate = nlapiStringToDate(newRecord.getValue('custrecord_created_date'));
  var endDate = nlapiStringToDate(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', nlapDateToString(tempEndDate));
}

这是 1.0 还是 2.0 脚本?

NetSuite 的 1.0 API 提供了一些可能对您有所帮助的日期操作方法:nlapiAddMonthsnlapiAddDays,以及日期-字符串转换方法。

这是您可以在 1.0 中执行的操作的示例

// 1.0 API does not pass scriptContext to saveRecord
function saveRecord() {

    // Use nlapiStringToDate instead of raw Date constructor
    var fromDate = nlapiStringToDate(nlapiGetFieldValue('custrecord_created_date'));

    // Instead of the full extra conditional, just use || as fallback
    var endDate = nlapiStringToDate(nlapiGetFieldValue('custrecord_end_date')) ||
            calculateEndDate(fromDate);

    // setting the value to the End Date Field
    nlapiSetFieldValue('custrecord_end_date', nlapiDateToString(endDate));
}

/** @param fromDate {Date} */
function addYear(fromDate) {
    return nlapiAddMonths(fromDate, 12);
}

/** @param fromDate {Date} */
function dayBefore(fromDate) {
    return nlapiAddDays(fromDate, -1);
}

/** @param startDate {Date} */
function calculateEndDate(startDate) {

    // add 1 year first, then subtract one day
    return dayBefore(addYear(startDate));
}

如果您使用的是 2.0,请添加评论,如果可以的话,我会尝试更新示例。如果您对它的工作原理有任何疑问,也请随时告诉我。