Jirong Hu Jirong Hu - 1 year ago 64
C# Question

How to compare to this date in C#?

Below is the string I got from a REST API to Sonatype Nexus. As I want to delete anything older than 60 days. How to write this code in C#?

<lastModified>2016-08-11 14:12:26.37 UTC</lastModified>

Answer Source

Assuming your API will always return <lastModified> and </lastModified> .. The following will do what you want.

var MyString = "<lastModified>2016-08-11 14:12:26.37 UTC</lastModified>";
MyString = MyString.Substring(14);
MyString = MyString.Replace("UTC", "");
MyString = MyString.Substring(0, MyString.Length - 15);

DateTime MyDateTime = new DateTime();
DateTime.TryParse(MyString, out MyDateTime);

if (MyDateTime < DateTime.Now.AddDays(-60) && MyDateTime != new DateTime(0001, 1, 1))
    //Do  Your Code
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download