Convert Bootstrap DateTime string to DateTime format using C# and VB.Net in ASP.Net

Last Reply on Oct 03, 2017 03:20 AM By dharmendr

Posted on Oct 02, 2017 01:13 AM

i have a text box on my page where user enter date as

dd/mm/yyyy ---> 15/10/2015 

d/m/yyyy ----> 1/10/2015

dd/m/yyyy --->  12/4/2015

and i want using that date searching data from sql so i need to convet that date to yyyy/mm/dd

or any other idea to handle this issue

please help me

thanks

Posted on Oct 03, 2017 03:20 AM

Hi yogeshc,

I have created sample. Refer the below sample.

SQL

CREATE TABLE SaveDateTimeFormat
(
	ID INT IDENTITY,
	ddmmyyyy DATETIME,
	dmyyyy DATETIME,
	ddmyyyy DATETIME,
	dmmyyyy DATETIME
)

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <style type="text/css">
        body
        {
            line-height: 0;
        }
        .dropdown-menu
        {
            font-size: 10px;
        }
    </style>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript" src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.1/css/bootstrap-datepicker.css" />
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.1/js/bootstrap-datepicker.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $('#txtDateTimeddmmyyyy').datepicker({ format: 'dd/mm/yyyy', autoclose: true });
            $('#txtDateTimedmyyyy').datepicker({ format: 'd/m/yyyy', autoclose: true });
            $('#txtDateTimeddmyyyy').datepicker({ format: 'dd/m/yyyy', autoclose: true });
            $('#txtDateTimedmmyyyy').datepicker({ format: 'd/mm/yyyy', autoclose: true });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div style="padding-left: 15px;">
        <div class="input-group date datepicker col-md-12 col-xs-12" >
            <input id="txtDateTimeddmmyyyy" class="form-control" type="text" runat="server" placeholder="dd/mm/yyyy" />
            <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
            </span>
            <input id="txtDateTimedmyyyy" class="form-control" type="text" runat="server" placeholder="d/m/yyyy" />
            <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
            </span>
        </div>
        <div class="input-group date datepicker col-md-12 col-xs-12" >
            <input id="txtDateTimeddmyyyy" class="form-control" type="text" runat="server" placeholder="dd/m/yyyy" />
            <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
            </span>
            <input id="txtDateTimedmmyyyy" class="form-control" type="text" runat="server" placeholder="d/mm/yyyy" />
            <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
            </span>
        </div>
        <asp:Button Text="Save" ID="btnSave" runat="server" CssClass="btn btn-default" OnClick="Save" />
    </div>
    </form>
</body>
</html>

C#

protected void Save(object sender, EventArgs e)
{
    DateTime dateTimeddmmyyyy = DateTime.ParseExact(txtDateTimeddmmyyyy.Value, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
    DateTime dateTimedmyyyy = DateTime.ParseExact(txtDateTimedmyyyy.Value, "d/M/yyyy", System.Globalization.CultureInfo.InvariantCulture);
    DateTime dateTimeddmyyyy = DateTime.ParseExact(txtDateTimeddmyyyy.Value, "dd/M/yyyy", System.Globalization.CultureInfo.InvariantCulture);
    DateTime dateTimedmmyyyy = DateTime.ParseExact(txtDateTimedmmyyyy.Value, "d/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.Connection = con;
            cmd.CommandText = "INSERT INTO SaveDateTimeFormat VALUES('" + dateTimeddmmyyyy + "','" + dateTimedmyyyy + "','" + dateTimeddmyyyy + "','" + dateTimedmmyyyy + "')";
            cmd.CommandType = System.Data.CommandType.Text;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}

VB.Net

Protected Sub Save(sender As Object, e As EventArgs)
    Dim dateTimeddmmyyyy As DateTime = DateTime.ParseExact(txtDateTimeddmmyyyy.Value, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture)
    Dim dateTimedmyyyy As DateTime = DateTime.ParseExact(txtDateTimedmyyyy.Value, "d/M/yyyy", System.Globalization.CultureInfo.InvariantCulture)
    Dim dateTimeddmyyyy As DateTime = DateTime.ParseExact(txtDateTimeddmyyyy.Value, "dd/M/yyyy", System.Globalization.CultureInfo.InvariantCulture)
    Dim dateTimedmmyyyy As DateTime = DateTime.ParseExact(txtDateTimedmmyyyy.Value, "d/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture)
    Using con As New SqlConnection(ConfigurationManager.ConnectionStrings(1).ConnectionString)
        Using cmd As New SqlCommand()
            cmd.Connection = con
            cmd.CommandText = "INSERT INTO SaveDateTimeFormat VALUES('" + dateTimeddmmyyyy + "','" + dateTimedmyyyy + "','" + dateTimeddmyyyy + "','" + dateTimedmmyyyy + "')"
            cmd.CommandType = System.Data.CommandType.Text
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
        End Using
    End Using
End Sub

Screenshot