Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i change Date Format in Month Day, Year in C#

Tags:

c#

I have a Following line of code. The Date of string type is in the format 29/11/2017. I wanted it as November 29, 2017. I tried adding String.Format but it display the same 29/11/2017 in pdf.

cellValDate.AddElement(new Phrase(String.Format("{0:dddd, MMMM d, yyyy}",
                       txtDate.Text, CultureInfo.CreateSpecificCulture("en-US")), 
                       new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 
                       10, iTextSharp.text.Font.NORMAL)));
like image 864
chetan Avatar asked Dec 19 '25 15:12

chetan


1 Answers

First you need to parse and convert the date string value in txtDate.Text to a DateTime object.

DateTime dt = DateTime.ParseExact(txtDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);

Then convert it to the particular format- "Month Day, Year".

string formateDate = dt.ToString("MMMM dd, yyyy");

Then you can pass it to your code-

cellValDate.AddElement(new Phrase(formateDate, 
                        new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 
                        10, iTextSharp.text.Font.NORMAL)));
like image 84
Display name Avatar answered Dec 23 '25 21:12

Display name



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!