Arianule
Arianule

Reputation: 9063

faster and more effective way changing date format

I have a list of users of which I want to change the Date format.

public class UserData
{
    public string DateAdded { get; set; }
}

private void FormatDateResult(List<UserData> users)
{
    foreach (var v in users.ToList())
    {
        if (v.DateAdded != null)
        {
            string temp = v.DateAdded.ToString();
            DateTime dAdded = DateTime.Parse(temp);
            v.DateAdded = dAdded.ToString("dd-MM-yyyy");
        }
    }
}

With allot of users it tends to be a bit sluggish. How can I do this in a better way?

Upvotes: 2

Views: 59

Answers (1)

Kien Chu
Kien Chu

Reputation: 4895

Maybe something like this looks better

public string DateAdded { get; set; }
public string FormattedDateAdded => DateAdded != null ? DateTime.Parse(DateAdded).ToString("dd-MM-yyyy") : DateAdded;

then you can use UserData.FormattedDateAdded anywhere you want

Upvotes: 3

Related Questions