# Parse Date to Specific Cultures (JavaScript)

**URL:** <https://forum.electroneek.com/t/parse-date-to-specific-cultures-javascript/3636>\
**Category:** Studio Pro\
**Created:** [December 19, 2023, 7:59pm UTC](https://forum.electroneek.com/t/parse-date-to-specific-cultures-javascript/3636 "2023-12-19T19:59:57Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Axel](https://forum.electroneek.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@Axel](https://forum.electroneek.com/u/Axel)\
**Post date:** [December 19, 2023, 7:59pm UTC](https://forum.electroneek.com/t/parse-date-to-specific-cultures-javascript/3636/1 "2023-12-19T19:59:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cris-dsc](https://forum.electroneek.com/user_avatar/forum.electroneek.com/cris-dsc/32/768_2.png) [@cris-dsc](https://forum.electroneek.com/u/cris-dsc)\
**Post date:** [December 20, 2023, 5:22pm UTC](https://forum.electroneek.com/t/parse-date-to-specific-cultures-javascript/3636/2 "2023-12-20T17:22:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Axel](https://forum.electroneek.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@Axel](https://forum.electroneek.com/u/Axel)\
**Post date:** [December 20, 2023, 5:45pm UTC](https://forum.electroneek.com/t/parse-date-to-specific-cultures-javascript/3636/3 "2023-12-20T17:45:40Z")

</div>

> [@cris-dsc](#):
>
> toLocaleString()

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.

---

<div class="post-metadata">

**Author:** ![cris-dsc](https://forum.electroneek.com/user_avatar/forum.electroneek.com/cris-dsc/32/768_2.png) [@cris-dsc](https://forum.electroneek.com/u/cris-dsc)\
**Post date:** [December 20, 2023, 8:54pm UTC](https://forum.electroneek.com/t/parse-date-to-specific-cultures-javascript/3636/4 "2023-12-20T20:54:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Muhammad\_Hayyan](https://forum.electroneek.com/user_avatar/forum.electroneek.com/muhammad_hayyan/32/1746_2.png) [@Muhammad\_Hayyan](https://forum.electroneek.com/u/Muhammad_Hayyan)\
**Post date:** [December 21, 2023, 5:44am UTC](https://forum.electroneek.com/t/parse-date-to-specific-cultures-javascript/3636/5 "2023-12-21T05:44:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Axel](https://forum.electroneek.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@Axel](https://forum.electroneek.com/u/Axel)\
**Post date:** [December 21, 2023, 12:53pm UTC](https://forum.electroneek.com/t/parse-date-to-specific-cultures-javascript/3636/6 "2023-12-21T12:53:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cris-dsc](https://forum.electroneek.com/user_avatar/forum.electroneek.com/cris-dsc/32/768_2.png) [@cris-dsc](https://forum.electroneek.com/u/cris-dsc)\
**Post date:** [December 22, 2023, 11:18am UTC](https://forum.electroneek.com/t/parse-date-to-specific-cultures-javascript/3636/7 "2023-12-22T11:18:05Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![Axel](https://forum.electroneek.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@Axel](https://forum.electroneek.com/u/Axel)\
**Post date:** [December 22, 2023, 2:13pm UTC](https://forum.electroneek.com/t/parse-date-to-specific-cultures-javascript/3636/8 "2023-12-22T14:13:08Z")

</div>

“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/](https://momentjs.com/)

---

<div class="post-metadata">

**Author:** ![cris-dsc](https://forum.electroneek.com/user_avatar/forum.electroneek.com/cris-dsc/32/768_2.png) [@cris-dsc](https://forum.electroneek.com/u/cris-dsc)\
**Post date:** [December 22, 2023, 3:57pm UTC](https://forum.electroneek.com/t/parse-date-to-specific-cultures-javascript/3636/9 "2023-12-22T15:57:20Z")

</div>

What if you follow [the instructions from the Help Center](https://docs.electroneek.com/docs/others#description)? This is a screenshot that might be useful:

 ![image](https://forum.electroneek.com/uploads/default/original/2X/f/fb58b45f17720ff410dc54b5151a8e9629f7482f.png)

---

<div class="post-metadata">

**Author:** ![Axel](https://forum.electroneek.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@Axel](https://forum.electroneek.com/u/Axel)\
**Post date:** [December 22, 2023, 6:12pm UTC](https://forum.electroneek.com/t/parse-date-to-specific-cultures-javascript/3636/10 "2023-12-22T18:12:19Z")

</div>

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).

 ![image](https://forum.electroneek.com/uploads/default/original/2X/1/11b6edf00ab37bae56f9bd8a2660dece9c090fd6.png)

---

<div class="post-metadata">

**Author:** ![cris-dsc](https://forum.electroneek.com/user_avatar/forum.electroneek.com/cris-dsc/32/768_2.png) [@cris-dsc](https://forum.electroneek.com/u/cris-dsc)\
**Post date:** [December 23, 2023, 2:20am UTC](https://forum.electroneek.com/t/parse-date-to-specific-cultures-javascript/3636/11 "2023-12-23T02:20:55Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Axel](https://forum.electroneek.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@Axel](https://forum.electroneek.com/u/Axel)\
**Post date:** [December 26, 2023, 8:18pm UTC](https://forum.electroneek.com/t/parse-date-to-specific-cultures-javascript/3636/12 "2023-12-26T20:18:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Muhammad\_Hayyan](https://forum.electroneek.com/user_avatar/forum.electroneek.com/muhammad_hayyan/32/1746_2.png) [@Muhammad\_Hayyan](https://forum.electroneek.com/u/Muhammad_Hayyan)\
**Post date:** [December 27, 2023, 5:13am UTC](https://forum.electroneek.com/t/parse-date-to-specific-cultures-javascript/3636/13 "2023-12-27T05:13:13Z")

</div>

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’);

---

<div class="post-metadata">

**Author:** ![Axel](https://forum.electroneek.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@Axel](https://forum.electroneek.com/u/Axel)\
**Post date:** [December 27, 2023, 1:37pm UTC](https://forum.electroneek.com/t/parse-date-to-specific-cultures-javascript/3636/14 "2023-12-27T13:37:21Z")

</div>

“require” is a method of node.js.

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