Can't get substring of current year

Does anyone know why either of these set of blocks crashes my project (legacy interface)?

I’m trying to convert 2020 to 20.

Reported at Getting substring from current year crashes project · Issue #786 · thunkable/thunkable-issues · GitHub.

Hey @tatiang - this is a fair assumption, but the “date” type is not the same as a “string” data type. Here’s a quick demo of how to type cast a date to a string in Thunkable.

The first thing I did was display the date in a label, like you’re doing:

Next, I tried to pass the year object to a sting function, in this case length() and got the same error you did:

Finally, I joined the year with an empty string (no characters, no spaces) to cast it to a string.

With this done, you can now use any string functions, including getSubstring() on the year.

Hope that helps!

1 Like

@domhnallohanlon I appreciate the quick response – and the explanation about the current year block returning a date type.

But I don’t understand what the blocks would look like to access the value of the current year using the length block.

I used this and it worked for me.

I used other characters and it worked fine but not an empty string.

3 Likes

Weird. Okay thank you, that works.

I just used the length () function because I knew as long as it returned 4 it was ok, once cast to a string you can then get the first two letters using your original approach. It was just sharing a bit about my debugging process I guess? :smiley:

1 Like

@muneer what are you storing in your ThisYear variable?

This seems odd - it worked fine for me. What happened when you used the empty string?

In this case if you wanted the last two characters you’d get “0#” instead of “20”, right?

I get that part but as soon as I replace the “length” block with anything else, it crashes the app again. So there’s something unique about the length block compared to the get substring block or join block by itself which crash as in these blocks I also posted above:

Very strange indeed. Here’s the project I made to replicate your issue:

Push and hold Button2 to see “20” returned.

Does that string contain a space?

Yours works. Mine doesn’t. No space, just an empty string “”

Edit: oh, now mine is working, too. Hmm. Okay, then!

Hello @domhnallohanlon,
The variable ThisYear is the block [current year].

In this case I will use in the substring from letter #3 to letter #4 which should bring “20” or if you run it after the new year then it should give “21”

My apologies for posting blocks to get the substring of the first two characters. I meant the last two (#3 and #4). It doesn’t change the issue at all but it makes more sense since, yes, I’ll want the “21” from 2021 next year. But it’s working for me now so I’m all set.

2 Likes