How to Extract Year component from DateTime and Convert to String for Concatenation

lawrence 136 Reputation points
2021-07-08T16:37:32.463+00:00

I need help please.
Can someone point me to what I am doing wrong in my code below in C#

string year = DateTime.Parse(DateTime.Now.ToString()).Year.ToString();

this code above works.

Why is the code below not working. I want to extract the year out of the DateTime Model(result.PolicyData.CommenceDate)

string year = DateTime.Parse(result.PolicyData.CommenceDate.Value.Year.ToString()).Year.ToString();

the error message is : String was not recognized as a valid DateTime.

Thank you in anticipation of your quick response.

Best regards

Lawrence

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | ASP.NET | Other
{count} votes

Accepted answer
  1. lawrence 136 Reputation points
    2021-07-08T20:57:34.373+00:00

    This line of code resolved the issue.

     var year = result.PolicyData.CommenceDate.Value.Year.ToString();
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.