首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果当前日期为1时,则(getDate() - 1)函数的值为零。

问如果当前日期为1时,则(getDate() - 1)函数的值为零。
EN

Stack Overflow用户
提问于 2016-09-07 04:11:24
回答 4查看 1.2K关注 0票数 0

My Requirement:

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

我的问题:

如果开始日期为"9/1/2016",而结束日期为空,则意味着应自动将结束日期值填充为"8/31/2016",但它会将结束日期值返回为"9/0/2016",并且我还将收到以下错误消息

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

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

代码语言:javascript
复制
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;
        }
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-09-07 04:25:42

不确定为什么您期望从1中减去1并得到除0以外的任何内容,但是您可以通过使用Date对象的setFullYear()和setDate()来解决这个问题。

​

代码语言:javascript
复制
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)));

​

票数 3
EN

Stack Overflow用户

发布于 2016-09-07 04:30:57

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

将一年添加到某个日期是相当简单的,但似乎您想要的日期是明年同一天之前的一天。所以加一年,再减去一天。

​

代码语言:javascript
复制
// 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日。

票数 1
EN

Stack Overflow用户

发布于 2016-09-07 04:39:01

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

关于阅读和设置日期字段的建议用法,请参见下文。

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39361078

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档