How do I find all ADTs where the discharge date (PV1-45) and message date (MSH-7) differ by 30 days?

Question: How do I find all ADT messages in a tab where the date/time of the message and the discharge date of the patient is 30 days or more?

To do this you need to need to parse the PV1-45 field into an HL7DateTime and compare it to the MSH-7 date time. There is a shortcut to the MSH-7 field on the message object called MessageDateTime which can be used instead of parsing the MSH-7 field.

 public override void Run()
 {
    // Get an HL7 Message in parsed format
    HL7Message message = GetParsedMessage();

    // if the message is not an ADT, or doesn't have a PV1 segment, skip it
    if(message.MessageCode != "ADT" || message.PV1 == null)
      return;
    
    if(message.MessageDateTime == null)
      return; // if we don't have a message date/time
    
    HL7DateTime dt;
    // try to parse the HL7 DTM
    if(!HL7DateTime.TryParse(message.PV1.DischargeDateTime_45.Value, out dt))
      return; // if we can't parse it successfully, skip it

    TimeSpan delta = dt.DateTimeOffset - message.MessageDateTime.Value;
    if(delta.TotalDays >= 30)
      SaveMessage(message, "old adt"); // save the message if it is older than our OldestDate
  }