I have strings of time (how poetic) from a third party and some have the timezone while others don't. What I'd like to do append a timezone if one isn't present.
Here are examples of strings with timezones...
5:21 AM MT
10:01 PM MT
8:41 PM MT
But sometimes that MT
is missing for whatever reason.
So, what I need to do is append MT
after AM
or PM
if it's missing.
(Note, I can't just check for MT
. It could be any timezone. The primary goal here is to be able to append any timezone I choose after AM
or PM
).
Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs.
Method 1: Concatenate Strings Into a String using the += operator. This operator can be used to perform this particular task of concatenating the string. This is quite simpler than the traditional methods that are employed in other languages, like using a dedicated function to perform this particular task.
If you want to add a string to the end of a string variable, use the += operator.
I didn't like any of these so I decided to just chomp
it off if it exists and then add it again. In your case:
"#{time.chomp(' MT')} MT"
Edit: this doesn't actually work for OPs edited use case (many different time zones) but is useful for both the original use case and potentially people finding this from Google (like me!).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With