[Solved] Convert yyyyMMdd date format to DateTime object in C# .Net

Last Reply on Dec 23, 2014 09:55 AM By Azim

Posted on Dec 22, 2014 09:44 PM

Mr.Muddassar.

My scenario is I have on text box with date (dd-MM-yyyy). I am using ajax in this text box as you explain in Ajax date format link

I need this date display into another text box with format yyyyMMdd on click the button

Because the invoice Number needs the yyyyMMdd-00001 when the user select the date.

How can do this.

Me.txtSoNo.text=me.txtSODate.text.tostring("yyyyMMdd")

error is

System.FormatException: Input string was not in a correct format.

Pls advice me how to do it

Thank you

maideen

Posted on Dec 23, 2014 09:55 AM

Please refer this code

HTML

<cc1:ToolkitScriptManager runat="server">
</cc1:ToolkitScriptManager>
<asp:TextBox ID="txtDate" runat="server" />
<cc1:CalendarExtender ID="ceDate" runat="server" TargetControlID="txtDate" Format="yyyyMMdd"
    PopupPosition="BottomRight">
</cc1:CalendarExtender>

<asp:Button Text="Save Sate" OnClick="SaveDate" runat="server" />

Namespace

using System.Globalization;

C#

protected void SaveDate(object sender, EventArgs e)
{   
    DateTime date = DateTime.ParseExact(this.txtDate.Text.Trim(), "yyyyMMdd", CultureInfo.InvariantCulture);
}