Unable to convert full 'DateTime' string into short 'Date' string

hello to everyone,

I am sending a date variable from a backend microservice in the format java.util.Date 2024-05-21T00:00:00 and this is then received by the bot in the following format Tue May 21 2024 00:00:00 GMT+0200 (Central European Summer Time)

how can I simplify this variable so the bot can read it as dd.MM.yyyy

I tried doing the following - DateTime.ParseExact(StartDate, "ddd MMM dd yyyy HH:mm:ss 'GMT'zzz", System.Globalization.CultureInfo.InvariantCulture).ToString("dd.MM.yyyy"))
and I’m getting the error String was not recognized as a valid DateTime.

thanks in advance!

Hi @Dea_Pahumi

Try this

inputString="Tue May 21 2024 00:00:00 GMT+0200 (Central European Summer Time)"
DateTime.ParseExact(inputString, "ddd MMM dd yyyy HH:mm:ss 'GMT'K '(Central European Summer Time)'", System.Globalization.CultureInfo.InvariantCulture).ToString("dd.MM.yyyy")

Regards,

1 Like

Hi @Dea_Pahumi

Try this:

startDate = "Tue May 21 2024 00:00:00 GMT+0200 (Central European Summer Time)"

modifiedString = DateTime.ParseExact(startDate.Substring(0, startDate.IndexOf(" GMT")), "ddd MMM dd yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture).ToString("dd.MM.yyyy")

Hope it helps!!

1 Like

So far this is not complete clear and maybe should be retaken, if needed

As the Timezone name is disturbing the parsing and also other parts are redundant, A regex Shortening can be tried:

[A-Za-z]+\s\d{2}\s\d{4}\s.+?(?=\s\()

So we can do:
grafik

And Parse:

But we recommend to als keep track of the offset and recommend to use the DateTimeOffset

Conversion into the desired format still can be done with toString

1 Like

thank you all, all 3 solutions worked for me @lrtetala @Parvathy @ppr I appreciate your help :pray:

1 Like

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