How to get just a year from string

Hi, I haven one problem. I have date in string variable and I need just a year.

I managed to get the year in a way: that i set new variable = DateTime.Parse(variable).ToString(“yyyy”), and that is working when i have double-digit moons, but when I have 01, 02, 03(month) I’m getting an error.

Please help

From which string are you trying to extract the year?. I think regex will be better in case of extracting the year from the specified string.

1 Like

If you have a full date string like “ddMMyyyy”, you can use Year(date.ParseExact(variable, “ddMMyyyy”, nothing)).ToString.
Like this, you can also make sure you have a correct date.

1 Like

@JOHOHO Can you provide the Input String that gave you the Error?