External API - Wrong Data Type

I’m using an API, which returns dates in a YYYYMMDD format.

"EstimatedArrival": {
                        "Arrival": {
                            "Date": "20190103",
                            "Time": "150000"
                        },

Bubble seems to be detecting this as some sort of date type (possibly unix time?) and will not let me pass the value 20190103 as a number or text. It will not correctly convert the date and returns a 1970 date if I attempt to do so in any of the various date formatting options.

Any idea what to do here?

Oh DUH. I didn’t check how the response datatypes were set in the connector. Must focus more on the details. :face_with_monocle:

I think u found your solution, but I had just been writing:

Are you talking about the Date key or the Time key?

The Date key’s value does represent an unambiguous date and Bubble will convert it to a date object (just select Date as its type when you initialize). That date object will be initialized to 00 hours (i.e., it represents the starting moment of Jan 3 2019).

As for the Time component, it’s unclear what/how that’s being reported, but your API docs will tell you. (Is “150000” fifteen hundred hours? i.e., 15:00:00 (hh:mm:ss) aka 3:00 PM). Whatever it is, you’ll have to parse that yourself and add it to the Date component to get a date/time object that reflects the actual arrival (if that’s what you desire to do).

The date key.

150000 is 15:00:00, that is correct.

Bubble does not interpret this date correctly and I get a Jan 1, 1970 date. I can import as text, but that’s not really workable as I need to perform date-related operations on it.

I’ve found find/replace regex to allow me to format the date in mm/dd/yyyy, but even so, as plain text there doesn’t seem to be a way to convert to a date object?

I also need this to function in an API workflow so having a plugin that runs on the page isn’t workable.

^^^ well, this is the solution to your problem. Example:

1 Like

That was what I had started out with. With the date type and feeding it the value of 20190103 causes bubble to return a Jan 1 1970 date.

Is there any server-side solution to dealing with this date format?

Ah, hrmm. I might be wrong about that format. What I know FOR SURE is this: YYYY-MM-DD does get interpreted properly by Bubble. Basically whatever API you’re interfacing to there is (to be blunt) dumb in its representations of date/times. Really lousy interface there.

Is there an option to control the formatting you get back from the API? Ideally you’d just get a simplified extended ISO text version of the date/time. (See for example my note here: How To Create A 'Date' from a Date String?)

Otherwise, what you need to do is import those things as strings and then convert them to dates yourself (moment.js can do this – you can tell it that the format is YYYYDDMM as it seems to be in your case and it’ll construct a proper date).

Unfortunately, no

How can I invoke moment.js as a server-side action to convert the string? This looks like an npm package, which I don’t think I can install on the server-side as of the last time I checked… I can’t do this on the page.

It sure does. Bubble seems to accept any date format that Javascript can deal with natively, which is quite a few. YYYYMMDD is not natively available so it needs to be converted to a format that bubble can deal with.

I successfully used a find/replace regex to format the date (as a string) in the right string, but bubble doesn’t deal with a string type for dates and there isn’t a way to convert it natively.

I’m wondering:
Is there a way to convert a properly-formatted string to a date in bubble editor?

.
.
MY WORKAROUND
I gave up on trying to use bubble’s editor for this and added a new server-side Javascript action to my plugin to parse and reformat the date string and pass it onto the API workflow as a proper date object.

MY API WORKFLOW
GET API RESPONSE > FORMAT DATE (NODE.JS) > OTHER DATE TASKS

Here’s the simple script

Plugin Editor: Actions (server-side)
Fields: date_in (type=string)
Returned Values: date_out (type=date)

function(properties, context) {
var dateString = properties.date_in
var year = dateString.substring(0,4);
var month = dateString.substring(4,6);
var day = dateString.substring(6,8);
var formattedDate = year + "-" + month + "-" + day; //yyyy-mm-dd format
var dateObject = {
date_out: formattedDate
};
return dateObject;
}

1 Like

(Your solution is the right one, really, based on where Bubble’s server-side support is right now. But I did have the following additional thought:)

I guess the only other question here is this: Do you really need those things to turn back into dates at import time?

As I think about this (again, really lousy) API response, I’m left with the impression that the designers of that interface were, in fact, NOT thinking about those things as dates and times but rather simply as string representations of dates and times designed for display.

If you don’t need to sort them as dates or do date-wise operations on them (that is, if they’re just for display purposes) they are fine (as your JS function above shows – it’s easy to stringwise parse them into “date DISPLAY” components when you need to).

I’m guessing this is some sort of transport-related API? Like, you’re querying flight 123’s arrival time? (But of course flight 123 leaves every day… so do you not already KNOW the date(s) of flight 123’s original departure and/or arrival?)

Its UPS and their API is ancient and lousy, but also rock-solid and will probably never change. I do need to modify the dates (add/subtract dates to calculate lead times) so display only is out of the question.

1 Like

Hello, I encountered a similar problem and was just wondering if this solution is still right? Any improvements in Bubble’s server side support that will make it any easier to manage dates returned as YYYYMMDD from an API call?