Is there any string escape using dateTime:FormatLocal/UniversalTime()?

Hi,

I’m wondering if it’s possible to escape using dateTime:FormatLocal/UniversalTime?

I’m currently trying to make a string with a prefix of ‘Recording’, and then some info about when the recording took place, however I’m having trouble doing so since the ‘d’ gets recognized as the elemental token ‘d’.

local s = now:FormatLocalTime('Recording-MMM-DD-YYYY_hh:mm:ss.SSSSA', 'en-us')
print(s) --> Recor3ing-Jun-07-2023_12:18:30.5975AM

I’ve tried using generic string library escapes (\ and %) to no avail.

local s = now:FormatLocalTime('Recor%ding-MMM-DD-YYYY_hh:mm:ss.SSSSA', 'en-us')
print(s) --> Recor%3ing-Jun-07-2023_12:18:30.5975AM
local s = now:FormatLocalTime('Recor\ding-MMM-DD-YYYY_hh:mm:ss.SSSSA', 'en-us')
print(s) --> Recor3ing-Jun-07-2023_12:18:30.5975AM

I’ve tried googling, checking the devhub etc but there aren’t many results, and the results that are there talk about different issues.

I know concatenation/string formatting is an option here but I want to avoid doing so if necessary. If there is not another option then so be it.

Any ideas?

You could use os.date(). I prefer os.date(“*t”) since it returns an array of stuff like year, day, month, hour, etc.

local dateArray = os.date("*t")

print(dateArray.month .. "/" .. dateArray.day .. "/" .. dateArray.year) -- Prints 6/6/2023 (Well for me, it might be different for you)

Concatenation can be messy, but it’s sometimes actually useful.

I want to avoid using os.date if possible as DateTime automatically localizes (actually os lib might but not 100% sure), has more options, allows for more precision etc.

So you know, you also don’t need to concatenate like this (and I’d advise against it as it’s slow since it reconstructs the string for every concatenation operator).

os.date('%m/%d/%Y') --> 6/7/2023

I know you’re trying to avoid concatenation, but you can use this:

local now = DateTime.now()
local s = now:FormatLocalTime('MMM-DD-YYYY_hh:mm:ss.SSSSA', 'en-us')
print('Recording-' .. s) -- Unlike earlier, ignore any d's in a string, and prints "Recording-Jun-07-2023_12:18:30.5975AM"

I find it a bit simpler, and there aren’t really any other apparent solutions.

As far as I know, there is no such way to escape these characters.

However, it may be possible to use some of those strange joiner characters found in the character map. Maybe it’ll work?

1 Like

Which joiner characters are you referring to?

U+200[x] characters to be precise. There might be more of them that OP could try, but I haven’t researched.

Edit: perhaps using a combination of joiner characters and full-width Latin characters?

Ehh honestly if those are the lengths I have to go to get an escape character I might just stick with string formatting. Thanks for the help!

Yeah. What’s weird is that they don’t have a replica of the letter d in the unicode set but they have whatever this is:
image

Also, perhaps using the full-space set would work: Recording (though it might not be suitable for your use case).

os.date returns the localized time for the client running it if that is what you mean.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.