简体   繁体   English

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

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

My Requirement: 我的要求:

I'm having to fields Start Date and End Date, If the End Date is left empty while saving the record, the End Date Field value is populated with plus 1 year based on the entered from date. 我必须输入“开始日期”和“结束日期”字段,如果在保存记录时“结束日期”留空,则“结束日期”字段值将根据输入的起始日期加上1年来填充。

My Issue: 我的问题:

If the Start Date is "9/1/2016" and the End Date is Left Empty means it should automatically populate the End Date value as "8/31/2016" but it returning the End Date value as "9/0/2016" and also i'm getting the following ERROR MESSAGE 如果“ 开始日期”为“ 9/1/2016” ,“结束日期”为空,则表示它应自动将“结束日期”值填充为“ 8/31/2016”,将“结束日期”值返回为“ 9/0/2016” ” ,同时我也收到以下错误消息

Error: JS_EXCEPTION INVALID_FLD_VALUE You have entered an Invalid Field Value Invalid Date for the following field: custrecord_end_date 错误:JS_EXCEPTION INVALID_FLD_VALUE您为以下字段输入了无效字段值无效日期:custrecord_end_date

CODE: SCRIPT : CLIENT SCRIPT, EVENT :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;
        }

Not sure why you expected to be able to subtract 1 from 1 and get anything other than 0, but you can solve this problem by using the Date object's setFullYear() and setDate() . 不知道为什么您希望能够从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))); 

Parsing strings with the Date constructor (and Date.parse, they are equivalent for parsing) is strongly recommended against since parsing is almost entirely implementation dependent and inconsistent. 强烈建议不要使用Date构造函数(和Date.parse,它们等效于解析)来解析字符串,因为解析几乎完全依赖于实现且不一致。 Manually parse strings with a custom function or use a library. 使用自定义函数手动解析字符串或使用库。

Adding a year to a Date is fairly simple, but it seems you want the date that is one day prior to the same date next year. 在日期中添加年份非常简单,但是似乎您想要的日期是明年同一日期的前一天。 So add one year then subtract one day. 因此,加一年然后减去一天。

 // 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()) 

Note that for 29 February, adding one year gives 1 May, then subtracting one day will give 28 February. 请注意,对于2月29日,加1表示5月1日,然后减去1天等于2月28日。

You should use the method nlapiStringToDate() for string to date conversions, as NetSuite gives date field value as string, which you must convert to date, and before you set back date, you must use nlapiSetFieldValue(YOUR_FIELD_ID, nlapiStringToDate(dateObject)) 您应该使用nlapiStringToDate()方法进行字符串到日期的转换,因为NetSuite将日期字段值作为字符串,您必须将其转换为日期,并且在设置回溯日期之前,必须使用nlapiSetFieldValue(YOUR_FIELD_ID, nlapiStringToDate(dateObject))

Please see below on suggested usage on reading and setting date fields. 请参阅下面有关阅读和设置日期字段的建议用法。

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

Is this a 1.0 or 2.0 script? 这是1.0还是2.0脚本?

NetSuite's 1.0 API offers a couple date manipulation methods that might be helpful to you here: nlapiAddMonths and nlapiAddDays , as well as the Date-String conversion methods. NetSuite的1.0 API提供了一些日期操作方法,可能在这里对您有帮助: nlapiAddMonthsnlapiAddDays以及Date-String转换方法。

Here's an example of what you could do in 1.0 这是您可以在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));
}

If you're using 2.0 just add a comment, and I will try to update the example if I can. 如果您使用的是2.0,请添加评论,如果可以的话,我将尝试更新示例。 If you've got any questions about how this works, feel free to let me know as well. 如果您对此有任何疑问,请随时告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM