Parse Date to Specific Cultures (JavaScript)

Hi, I’m trying to parse a Date to a specific format (language/culture) without having to do lots of manual manipulations of the data, so I wanted to use the library “momentjs”.

However, I tried many forms and I couldn’t import the library to use it. You can’t import libraries for JavaScript in Electroneek?.

I know you can use “Moment” inside the IDE, but I have to do other stuff inside my iteration (which runs inside of a “.js” file), and one of those things requires me to parse dates in each iteration, so I need to do it inside the “.js” file, which is called from the IDE with an “Execute JavaScript”.

Thank you.

What format(s) exactly do you need to parse the dates? Perhaps you can use toLocaleString() or something like that. I always use it, or some other similar method, and it works great for me.

It always depends on the process.

Sometimes I might receive something like “September 3, 2023” or “03/09/2023”, or “3 Sep. 2023”.

That’s why I need flexibility.

toLocaleString() only works with already defined “Date” variables, but I need to define the variable first, inside the “.js” file, with the corresponding format specific to the logic of the proccess.

So you receive dates in several different formats.
Do you need to parse these dates into one specific format or several formats?

You can use GPT- 4 for this scenario. It can give you the best result if you use good prompts.

I need to parse them in the format of the logic of the process. For example for the one I’m working on right now, I receive the dates as strings (in bulk along with other data), with the format “September 3, 2023”.

And I need to parse that into a “Date” variable so I can do what I need with it.

I prompted ChatGPT and this is the answer it generated. See if it helps you:

function parseToDate(dateString) {
  const formats = [
    'YYYY-MM-DD', // Example: '2023-12-31'
    'DD/MM/YYYY', // Example: '31/12/2023'
    // Add other formats you need to support
  ];

  for (let format of formats) {
    const date = moment(dateString, format, true); // Using moment.js for parsing

    if (date.isValid()) {
      return date.toDate(); // Return Date object if conversion succeeds
    }
  }

  return null; // Return null if no format matches the date
}

// Example date strings
const dateString1 = '2023-12-31';
const dateString2 = '31/12/2023';

// Convert strings to Date objects
const date1 = parseToDate(dateString1);
const date2 = parseToDate(dateString2);

console.log(date1); // Output: Wed Dec 31 2023 00:00:00 GMT+0000 (Coordinated Universal Time)
console.log(date2); // Output: Wed Dec 31 2023 00:00:00 GMT+0000 (Coordinated Universal Time)

If you have multiple dates in different formats and need to convert them to Date objects, you can approach this by creating a function that attempts to parse the date string in various formats. This example uses the moment.js library to facilitate parsing different date formats. You can add more formats to the formats array as needed to accommodate the different patterns of your dates. Ensure you add the formats in the priority order you expect to encounter them.

“Moment” is not defined inside that “.js” code. If you copy that “.js” and you execute it using the activity “Execute JavaScript”, you will get an error saying that “the variable moment is not defined”.

That’s why I need to know if moment can be used inside the “.js”. And how can I define it (import it’s .js).
https://momentjs.com/

What if you follow the instructions from the Help Center? This is a screenshot that might be useful:

I already did a few days back and they didn’t work with the “Execute JavaScript” activity (which is the one I need to use, like I said from the beggining).

Oh, I got confused with the javascript activities and thought you were talking about the other one.
I prompted ChatGPT again and it suggested the following code, without the use of moment.js:

function parseDateString(dateString) {
  const formats = [
    // Add here the formats you expect to encounter
    /^\d{4}-\d{2}-\d{2}$/, // Example: '2023-12-31'
    /^\d{2}\/\d{2}\/\d{4}$/, // Example: '31/12/2023'
    // Add other formats you need to support
  ];

  for (let format of formats) {
    if (format.test(dateString)) {
      const [year, month, day] =
        dateString.split(/[-/]/).map(item => parseInt(item, 10));
      const parsedDate = new Date(year, month - 1, day); // Month is 0-indexed

      if (!isNaN(parsedDate.getTime())) {
        return parsedDate; // Return the Date object if parsing is successful
      }
    }
  }

  return null; // Return null if no format matches the date
}

// Example date strings
const dateString1 = '2023-12-31';
const dateString2 = '31/12/2023';

// Parse the strings to Date objects
const date1 = parseDateString(dateString1);
const date2 = parseDateString(dateString2);

console.log(date1); // Output: Wed Dec 31 2023 00:00:00 GMT+0000 (Coordinated Universal Time)
console.log(date2); // Output: Wed Dec 31 2023 00:00:00 GMT+0000 (Coordinated Universal Time)

This code defines a function, parseDateString , that uses regular expressions to match different date formats. It then attempts to split the date string into year, month, and day components and creates a Date object based on those components. Adjust the regular expressions in the formats array to match the specific formats you expect to encounter.

If this doesn’t work for you, I don’t know what will.

The question was “Can I import libraries for JavaScript in Electroneek?. If so, how can I Import Momentjs()?”

Have you tried this?

// Example of including Moment.js in your Custom Activity
const moment = require(‘path/to/moment.js’);

// Now you can use Moment.js functions in your code
const currentDate = moment().format(‘YYYY-MM-DD’);

“require” is a method of node.js.

To use it you must first import “node.js”.