If I wanted to take a transaction date and subtrac...
# suitescript
t
If I wanted to take a transaction date and subtract 7 days from that dat in SuiteScript, How would I do that? I've tried the following and the result that I get is in milliseconds, not in days. This is for a client script on the TimeSheet record.
Copy code
var tranDate = rec.getValue('trandate'); //result - Sat Apr 30 2022 00:00:00 GMT-0600 (Mountain Daylight Time)
            var tranDateNew = new Date(tranDate); //result - Sat Apr 30 2022 00:00:00 GMT-0600 (Mountain Daylight Time)
            var tranDateMinus7 = tranDateNew.setDate(tranDateNew.getDate() - 7); // result - 1650693600000. I'm hoping to get Apr 23 2022
b
setDate does not function as a chainable method
t
So how do I get a date result instead of milliseconds?
b
do you understand the side effects of using the method
t
I guess not. The documentation shows a result that shows the day of the month (24 for Augst 24 1975), but the results I'm getting are in milliseconds. I want to take Sat Apr 30 2022 000000 GMT-0600 (Mountain Daylight Time) and convert it to April 23 2022.
b
whats your understanding of the first sentence
Copy code
The setDate() method changes the day of the month of a given Date instance, based on local time.
t
I take that to mean that if the date I was evaluating used the setDate method, then the day of the month would be changed. For example, tranDate = April 23 2022 would become 24 if I performed trandate.setDate(1). But that isn't happening when I try that code. Instead, I get a much larger result.
I guess that method may actually perform something different. In my above example, would it reset the date portion of that date from the 24th of April 2022 to the 1st of April 2022?
b
it sets, it doesnt add
t
And my question is, how do I subtract days from the transaction date provided from a NetSuite record?
b
you already had the code for that
Copy code
tranDateNew.setDate(tranDateNew.getDate() - 7)
after that code, tranDateNew is going to be a date 7 days younger
t
That appears to return the number 23 rather than April 23 2022. I tested it again but subtracted 60 days, and I got a result of -30 instead. My intent is to be able to take a date, and perform a search that uses the prior 7 days of that date. I don't see how that might help just yet, especially if those dates cross two different months.
b
Copy code
tranDateNew.setDate(tranDateNew.getDate() - 7)
will not return 23 or -30
it should return a much larger number
Copy code
tranDateNew.getDate() - 7
would return 23, but thats not very interesting by itself
you are going to need to understand what a Mutator method is
there are variants of mutators that are chainable, in that the return value of the mutator is used to return the variable so that you can chain multiple method together, for example
Copy code
tranDateNew.setDate(1).setMonth(1)
the Date setters are not chainable, they do not work that way
they still are a mutator, so tranDateNew will be modified after
Copy code
tranDateNew.setDate(1)
t
I guess that's where I'm confused. I'm not trying to manipulate the transaction date (April 30 2022), but rather to use that as a reference to create a second date (April 23 2022).
Another way of saying this is that I want to create an offset date (transaction date - x days)
b
then you actually need to make a new date
t
I didn't realize that I was manipulating the original date. I'll give that a try.
c
Congrats, you have been burnt by dates in JS, this happens to everyone. If you're doing anything more than the most basic of date operations, you should use a third party library.
I've used moment.js - I believe this one is deprecated now though. I hear Day.js is a good replacement @Tim_Pedersen
👍 1
t
I guess so. Is offsetting a date by 7 days supposed to be this complex. I just want to find out what the full date was a week before my provided date.
c
@Craig FYI moment isn't deprecated, its just "complete" and they aren't adding to it. I still use it in projects and they say go for it as well. If you want the latest and greatest you move to Luxon (https://moment.github.io/luxon/#/). I see no reason to have to stop using moment at all. Its super useful.
@Tim_Pedersen with something like moment, you'd do "new moment(<your_date_object_here>).subtract(7, 'days').toDate();" and there ya go.
b
choose moment if you choose a date library, things like luxon or dayjs require you to polyfill the Intl apis if you want timezone support, moment-timezone is old enough to have included timezone information
t
Thanks everyone.
c
I haven't used the moment timezone at all yet. I've used moment w/ N/format which handles the time zone stuff. Maybe there's a better way then?