Nicely done.
You can save yourself some text munging by getting the enddate directly, using dateopt to format it in ISO 8601 format. For example:
echo | openssl s_client -showcerts -connect wiki.keyboardmaestro.com:443 2>/dev/null | openssl x509 -inform pem -noout -enddate -dateopt iso_8601
...returns:
notAfter=2025-10-02 23:16:09Z
Edit to add
It seems dateopt is only available for some versions of openssl x509 -- it's in OpenSSL 3.3.1 on my Air but not LibreSSL 3.3.6 on my iMac.
Unfortunately I have no idea which is the default macOS-installed one (I'll keep digging), so tread carefully!