Could anyone please help me to change a string to ...
# suitescript
v
Could anyone please help me to change a string to date format?
s
If (and only if) the string is in the default date format, then you could use format.parse from the N/format module in SS 2.x: https://suiteanswers.custhelp.com/app/answers/detail/a_id/44523 If it’s in some other format, you’d be better off using a Date library like JS-Joda or MomentJS to parse the date with a specified format string.
v
how to use JS-Joda or MomentJS? should i install something?
s
you would need to put the library file somewhere in your SuiteScripts file cabinet folder or subfolder, then reference it in the usual RequireJS manner. Moment is a single JS file. I can’t recall if that is also the case for JS-Joda. Moment is fairly popular in SuiteScript projects, though it is technically deprecated now.
a
message has been deleted
depends on your locale and if you can trust the date format
also what format is the string in? does it have a time as well? a time zone? generally when asking for help you want to provide as much info as possible.
v
Hello All, I have a date as March 8 1985. I want to convert it to 8 March 1985. How can this be achieved? Could anyone please help? Already tried parse, but it was not success
s
If every date is of that format, then I’d just do this:
Copy code
const [monthName, day, year] = dateString.split(' ');
const newDate = `${day} ${monthName} ${year}`;
That would require the script to be SuiteScript 2.1
it doesn’t sound like you really need or even want it as an actual date, just to reformat the date string, so no need for a library
v
I used moment it worked fine. thank you
Copy code
const [monthName, day, year] = dateString.split(' ');
const newDate = `${day} ${monthName} ${year}`;
This one is showing error on ´´ character. what can i do.?
j
If you're not using SS2.1 you'd need to refactor with something like this:
Copy code
var dateArray = dateString.split(' ');
var newDate = dateArray[1] + ' ' + dateArray[0] + ' ' + dateArray[2];
s
@Jordan Patterson thank you nice, that is probably the simplest way to do it in ES5. If possible, I would consider upgrading 2.0 scripts to 2.1, for the added benefits of the new features in ES6 - ES10 and beyond. They will mostly work the same, but there are a few differences, particularly with Error handling, that need to be considered. If this is a new script, I would always start with 2.1
j
@scottvonduhn Absolutley agree! And for those wanting even more, throw TypeScript in with 2.1.
s
Not to mention, the other benefit of 2.1 over 2.0 is that is uses a newer and actively developed JS engine, while SS 1.0 and 2.0 use a very old and deprecated JS to Java converter.
👍 1
a
huh... is 2.0 really still running on rhino? I just assumed they moved everything to GraalVM... is that actually documented anywhere?