Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript (getDate()-1)函数在当前日期为1时获取值零_Javascript_Date_Netsuite_Suitescript - Fatal编程技术网

Javascript (getDate()-1)函数在当前日期为1时获取值零

Javascript (getDate()-1)函数在当前日期为1时获取值零,javascript,date,netsuite,suitescript,Javascript,Date,Netsuite,Suitescript,我的要求: 我必须输入“开始日期”和“结束日期”字段,如果保存记录时“结束日期”字段为空,则“结束日期”字段值将根据输入的起始日期加上1年 我的问题: 如果开始日期为“9/1/2016”且结束日期为空,则意味着它应自动将结束日期值填充为“8/31/2016”,但它将结束日期值返回为“9/0/2016”,并且我收到以下错误消息 错误:JS_异常 无效\u FLD\u值您为以下字段输入了无效字段值无效日期:custrecord\u end\u Date 代码: 脚本:客户端脚本,事件:SaveRec

我的要求:

我必须输入“开始日期”和“结束日期”字段,如果保存记录时“结束日期”字段为空,则“结束日期”字段值将根据输入的起始日期加上1年

我的问题:

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

错误:JS_异常 无效\u FLD\u值您为以下字段输入了无效字段值无效日期:custrecord\u end\u 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()
来解决此问题

函数addingplusEarOfCurrentDate(fromDate){
var日期=新日期(fromDate);
date.setFullYear(date.getFullYear()+1);
date.setDate(date.getDate()-1);
返回日期;
}

console.log(添加当前日期(新日期(2015年10月1日))
不确定为什么希望能够从1中减去1并得到除0以外的任何值,但您可以通过使用
Date
对象的
setFullYear()
setDate()
来解决此问题

函数addingplusEarOfCurrentDate(fromDate){
var日期=新日期(fromDate);
date.setFullYear(date.getFullYear()+1);
date.setDate(date.getDate()-1);
返回日期;
}

console.log(添加当前日期(新日期(2015年10月1日))由于解析几乎完全依赖于实现且不一致,因此强烈建议禁止使用日期构造函数解析字符串(和Date.parse,它们在解析时是等效的)。使用自定义函数或库手动解析字符串

将年份添加到日期相当简单,但您似乎希望日期比明年的同一日期早一天。所以加上一年,然后减去一天

//将m/d/y格式字符串解析为日期并验证结果
函数parseMDY(s){
var b=s.split(/\D/);
var d=新日期(b[2],--b[0],b[1]);
返回d&d.getMonth()==b[0]?d:新日期(NaN);
}
//给日期加上1年
功能添加年份(d){
if(Object.prototype.toString.call(d)!='[Object Date]')返回;
d、 setFullYear(d.getFullYear()+1);
d、 setDate(d.getDate()-1);
返回d;
}
var d=parseMDY('9/1/2016');
console.log(d.tolocalString())
第二年(d);

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

将年份添加到日期相当简单,但您似乎希望日期比明年的同一日期早一天。所以加上一年,然后减去一天

//将m/d/y格式字符串解析为日期并验证结果
函数parseMDY(s){
var b=s.split(/\D/);
var d=新日期(b[2],--b[0],b[1]);
返回d&d.getMonth()==b[0]?d:新日期(NaN);
}
//给日期加上1年
功能添加年份(d){
if(Object.prototype.toString.call(d)!='[Object Date]')返回;
d、 setFullYear(d.getFullYear()+1);
d、 setDate(d.getDate()-1);
返回d;
}
var d=parseMDY('9/1/2016');
console.log(d.tolocalString())
第二年(d);

console.log(d.toLocaleString())
对于字符串到日期的转换,您应该使用方法
nlapistingToDate()
,因为NetSuite将日期字段值作为字符串提供,您必须将其转换为日期,并且在设置回日期之前,必须使用
nlapistingFieldValue(您的字段ID,nlapistingToDate(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));
}

对于字符串到日期的转换,您应该使用方法
nlapiStringToDate()
,因为NetSuite将日期字段值作为字符串提供,您必须将其转换为日期,并且在设置日期之前,必须使用
nlapiSetFieldValue(您的字段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.0API提供了一些可能对您有所帮助的日期操作方法:
nlapiAddMonths
nlapiAddDays
,以及日期字符串转换方法

下面是一个在1.0中可以实现的示例

//1.0 API未将scriptContext传递给saveRecord
函数saveRecord(){
//使用nlapiStringToDate而不是原始日期构造函数
var fromDate=nlapiStringToDate(nlapiGetFieldValue('custrecord\u created\u date');
//使用| |作为回退,而不是完整的附加条件
var endDate=nlapiStringToDate(nlapiGetFieldValue('custrecord\u end\u date'))||
计算日期(起始日期);
//将值设置为“结束日期”字段
nlapiSetFieldValue('custrecord_end_date',nlapiDateToString(endDate));
}
/**@param fromDate{Date}*/
函数addYear(fromDate){
返回nlapiAddMonths(从12日起);
}
/**@param fromDate{Date}*/
函数dayBefore(fromDate){
返回nlapiAddDays(从日期开始,-1);
}
/**@param startDate{Date}*/
函数calculateEndDate(startDate){
//先加一年,然后减去一天
返回前一天(addYear(startDate));
}
如果您使用的是2.0,只需添加一条注释,如果可以,我将尝试更新示例。如果您对这项技术的工作原理有任何疑问,请随时告诉我。