hi everyone, I am looking into checking if a parti...
# suitescript
k
hi everyone, I am looking into checking if a particular date is within the next 365 days in suitescript 2.0. I tried using the following, but looks like NS does not support
getTime()
. Any thoughts on how can this be achieved?
var time_diff = Math.abs(d1.getTime() - d2.getTime());
var days_diff = time_diff / (1000 * 60 * 60 * 24);
where d1 and d2 are 2 different dates
k
.getTime i have used before so should be available
are D1 and D2 are date objects?
k
I am getting this error:
TypeError: Cannot find function getTime in object
k
have you logged the d1 and d2? if you have what do the logs state?
Also would be an idea to post more of the code with the date object variables
k
d1 and d2 are date values being extracted from a line level SO field. I am using this function in a map-reduce script and the function is outside of the reduce stage: here is the function:
Copy code
//helper function to calculate date difference
        function numDaysBetween(d1, d2) {

            var time_diff = Math.abs(d1.getTime() - d2.getTime());
            var days_diff = time_diff / (1000 * 60 * 60 * 24);

            return days_diff;
        };
k
That may be the issue yo may need to change it back to an object. I would log the typeof for d1 and d2 and see if they state an object.
s
NS has no "problem" with
getTime()
, the problem is your objects are not js Date objects.
c
use momentjs
💯 3
var oneYearFromToday = new moment(new Date()).add(365, 'days');
var myDate = new moment(new Date());
var durationInDays = new moment.duration(oneYearFromToday.diff(myDate)).as('days');
var isValid = (durationInDays <= 365);
console.log('Duration: ' + duration);
console.log('Is Valid: ' + isValid);
k
hi @creece thank you for sharing this snippet! right now I am pulling
myDate
from line level of a SO, and it seems to be of type string. Does it need to be converted to date object?
k
You could just do
new Date(mydate)
which would change it into a date object
IMO
c
N/format module if you're using serve-side scripting and JUST new Date() so you get it in the correct timezone. if using an actual existing date, new Date(<date here>) should be fine.
k
Untitled.js
its a map-reduce, so yes its a server-side: and I am using it like above, but right now I get this error:
RangeError: Date is invalid.
I believe in line 8
start_date
when pulled from the sublist is of type string right now
k
what is
start_date
logging as? the actual value
c
should be a date object if the field is of type date
k
looks like the field is of type date/time. Its logging like this:
2021-05-01T07:00:00.000Z
c
its never getting to either log? Your posted code looks fine.
k
I think I was getting error because some of the date I pulled were blank, I am excluding them and running again
yup that was it! Thank you so much all!