Format a date in a Data Directive procedure

I’ve got a Data Directive that sends out an email when a Packer is marked shipped (or when the Tracking# changes).

I’ve recently added the Shipping date to the email’s body, but the format includes a time of “12:00:00 AM”

Here’s the code:

NewEmailBody = NewEmailBody + "<b>Shipment Date: ";
NewEmailBody = NewEmailBody + ttShipHead_Row.ShipDate + "</b><br><br>";

That yields:

How do I format ttShipHead_Row.ShipDate as mm/dd/yyyy ?

Since it’s stored as a datetime, you can try formatting the datetime as a string with the format you want.
ttShipHead_Row.ShipDate.ToString("MM-dd-yyyy")

Thanks. I tried

ToString("d")

per the following MS example

Console.WriteLine(thisDate.ToString("d")); // Displays 3/15/2008

I’ll give the full “dd/MM/yyyy” a try

There are many ways to format your date, it’s just a matter of picking the one you want for the scenario.

https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx

http://www.csharp-examples.net/string-format-datetime/

I get “There is at least one compilation error.” when trying to save with the .ToString(…)

What is the error

With

I get an error when trying to save the Data Directive

Try creating your string variable first, then using the String.Format method to modify the string as @Mark_Wonsil provided above.

string newDate = ttShipHead_Row.ShipDate.ToString();
newDate = String.Format("{0:M/d/yyyy}", newDate);
1 Like

That saves/compiles okay. But still displays as

Are you displaying your new variable or are you still displaying ttShipHead_Row.ShipDate?

Yup.

And I’m sure the email I got was from a packer edited after the changes (I tweak the Tracking number)

EDIT:
Why is newDate being declared as a string ?

Just to be clear. This is in the Custom Code of a Data Directive’s “Execute Custom Code” block

Can you try this:
string newDate = String.Format("{0:d}", ttShipHead_Row.ShipDate);

Did you disable, re-enable the BPM in order to force a save of the new code?

No. I never do that. Should I always do that? Or are you thinking some sort of glitch?

Think you replied to the wrong comment :slight_smile:

1 Like

I replied to the right comment, but I had Your post highlighted.

string newDate = String.Format("{0:d}", ttShipHead_Row.ShipDate);

This finally worked.

I’m glad it took others some time to figure it out. Now I don’t feel so alone :wink:

Hah good, such a simple one too!! Wasn’t thinking straight, otherwise we could have solved this in one post.