ASP.NET - Formating Date/Time from codebehind

Hi Experts,

I need to display formatted date/time from codebehind, I am doing the following but it doesn't show the proper time:

lblValueDate.Text = Format(CType(DV.Table.Rows(0).Item("ValueDate").ToString, DateTime), "hh:mm tt, dd - MMMM - yyyy")

Although, similar code works fine when assigned from the markup to a grid on the same page:

<asp:Label ID="lblValueDate" runat="server" CssClass="note"
                text='<%# "Posted " & Format(DataBinder.Eval(Container, "DataItem.ValueDate"),"hh:mm, dd - MMMM - yyyy") %>'></asp:Label>
Who is Participating?
Imran Javed ZiaConnect With a Mentor Consultant Software Engineer - .NET ArchitectCommented:
Please use as:
lblValueDate.Text =
CType(DV.Table.Rows(0).Item("ValueDate").ToString, DateTime).ToString( "hh:mm tt, dd - MMMM - yyyy")

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.