user3585325
user3585325

Reputation: 79

How to get difference between two dates in netsuite?

I m new to netsuite ,i have to find difference between two dates in netsuite. How can i get the difference of two dates in netsuite through scripts.help me,Thanks.

Upvotes: 5

Views: 17909

Answers (3)

2ps
2ps

Reputation: 15956

for those using suiteql, make sure to cast to a number using to_number:

select to_number(lastmodifieddate - createdate) as delta
from transaction
where type = 'SalesOrd'

Upvotes: 0

Rockstar
Rockstar

Reputation: 2288

(I) Get the Difference of Two Date Fields Using an SQL Expression

  1. Navigate to Customization > Lists, Records, & Fields > Entity Fields > New
  2. Create the Custom Fields

Custom Field 1.

Label = Date Created
ID: datecreated
Type: Date
Store Value: True
Applies To: Customer
Display > Subtab: Main

Custom Field 2.

Label: Date Closed
ID: dateclosed
Type: Date
Store Value: True
Applies To: Customer
Display > Subtab: Main

Custom Field 3.

Label: Days Open
Type: Date
Store Value: False
Applies To: Customer
Display > Subtab: Main
Validation and Defaulting > Formula: T
Formula field: {dateclosed}-{datecreated}

  1. Pull up any Customer record > Fill out the custom fields.

For example:

Date Created = 9/1/2014
Date Closed = 9/3/2014

  1. Click Save.
  2. Days Open field should show the value of 2.

(II) Calculate the Difference between a Date/Time field and a Date field Using Formula

The following formula will give you the difference in days. You can add this to the results tab of your saved search.

Formula(Numeric): ROUND({ID of date field}-{ID of other date field})

Note: Either field can be used for the date/time field value.

(III) Using SuiteScript Code

function date_difference(date1, date2) {
    var timeDiff = Math.abs(date2.getTime() - date1.getTime());
    var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); // Get 1 day in milliseconds
    return diffDays;
}

Upvotes: 6

felipechang
felipechang

Reputation: 924

You can convert a Text date ('01/01/2014') to a Date object with nlapiStringtoDate.

So basically you go

timeDiff = Math.abs(nlapiStringtoDate(myDate).getTime() - (new Date).getTime()) diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));

Upvotes: 2

Related Questions