question

briggzee-5832 avatar image
0 Votes"
briggzee-5832 asked briggzee-5832 commented

Formatting date for webform mail message

In my ASP.Net VB app I have an tournament entry form that uses birth date. I pass the date to the database in the following format yyyy-mm-dd and it works fine. In the web form mail message I send to the applicant I want the birth date to display as MMM-dd-yyyy (eg Jan 1 2022). In the code behind I've tried various methods to re-format the date but keep getting various errors. The line of code in the mail message body is the following.
"<p>BIRTH DATE: " & HttpUtility.HtmlEncode(BirthDate) & "</p>" & .

Any help will be appreciated.

dotnet-aspnet-webforms
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Yes I convert in the code behind but am receiving the following error when I try your solution:

System.InvalidCastException:
Unable to cast object of type 'System.String' to type 'System.IFormatProvider'.

using the following code:

"<p>BIRTH DATE: " & HttpUtility.HtmlEncode(BirthDate.ToString("MM-dd-yyyy"))

0 Votes 0 ·

The BirthDate variable is defined as a string. Convert the string to a DateTime using DateTime.TryParse() then back to a string. Or, fix the general design and use a DateTime throughout.


1 Vote 1 ·

Thanks, I changed the design and it's working fine now.

0 Votes 0 ·

1 Answer

YijingSun-MSFT avatar image
1 Vote"
YijingSun-MSFT answered

Hi @briggzee-5832 ,
Do you care of convertering data format in the code behind? You could use this:

 xxxxx.ToString("MM-dd-yyyy")

Best regards,
Yijing Sun


If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.