Save a String (as Date) in Database

Hi all,

how to solve this problem?

I use a backend workflow to store data in the database:

item #2 is a TEXT: “01/15/2024 12:00 am" and I want to save it in the db.

How to convert a TEXT into a valid DATE?

“Start Date” needs a DATE format.

I dont find something like “myText:convert to date”

please help!

thanks,

jupxi

Text = 15/02/2025

In my case, dd/mm/yyyy

Date = currentDate replace date to text.splited by "/" Item1 : Converted to number replace month to text.splited by "/" Item2 : Converted to number replace year to text.splited by "/" Item3 Converted to number

there is no “replace date to.,.,.”:

And I also using 12:00 am

Sorry:

Hi @jupixy

There is no standard Bubble operation to convert a string to a date that I know of. However, there are plugins which can help. Such as “Text to Unix”:

After installing the plugin, you will need to add two new Workflow steps. The first is “Convert Text to Unix”, which will convert the string you have received “01/15/2024 12:00 am” to a Unix data type.

You can then use “Unit to Date” which will convert the Unix string to a date type bubble understands:

Finally, use the result of the “Unit to Date” as the Bubble date when you update the database:

There are also approaches using Javascript and the ToolBox plugin as explained here:

No need to take such long routes to solve this problem. Just ask ChatGPT to whip up some custom javascript and let it do it for you!

Here’s what I did in under 5 minutes: ChatGPT - JavaScript date to Unix

Step by Step:
:small_blue_diamond: Step 1: Add a JavaScript to Bubble element

  • Drag “JavaScript to Bubble” onto your page.

  • Give it a name like js_to_bubble.

  • Make sure “Publish value” is checked.

  • Set Value type to “Date”

:small_blue_diamond: Step 2: Add this script in a Run JavaScript action (add this action when your date is ready. Make sure to replace the date string at the end of the script by making it dynamic)

function toUnixTimestamp(dateString) {
  // Parse date string in format: MM/DD/YYYY hh:mm am/pm
  const date = new Date(dateString);

  if (isNaN(date.getTime())) {
    bubble_fn_invalid && bubble_fn_invalid("Invalid date format. Use MM/DD/YYYY hh:mm am/pm");
    return;
  }

  // Convert to Unix timestamp (seconds)
  const unixTimestamp = Math.floor(date.getTime() / 1000);

  // Send result back to Bubble
  bubble_fn_js_to_bubble(unixTimestamp);
}

// Example: Replace this with your Bubble input value
const userDate = "01/15/2024 12:00 am";
toUnixTimestamp(userDate);

:small_blue_diamond: Step 3: Connect it to Bubble

  • Whenever you have your date ready, add a workflow to “Run Javascript”

  • In my test, I’ve added this on the click of a button and am displaying it in a textbox, feel free to customise it as per your need

PS. You need the Toolbox plugin for this

simplest solution is this

  • install toolbox plugin
  • drop expression element in page, somewhere snug
  • set result type as date
  • use this expression {“01/15/2024 12:00 am”}
    and thats it, bubble sees this as proper date data type, access it from Expression’s:value

this might be useful to you, it supports plugin element for no WU usage in frontend, and a server-side action for use in backend in a workflow [🦄 New plugin] - Better Date Parser

alternatively you could try calling your own backend workflow and returning data from that as when the backend workflow receives its input, it’ll parse any date into a ‘bubble’ date

If this is on a page…the absolute simplest thing (ie: no plugins) is to just send the string into a url parameter and when saving to the db use ‘get data from url’ reference the correct parameter name and set the type to ‘date’…Bubble handles the rest.

3 Likes

This topic was automatically closed after 70 days. New replies are no longer allowed.