Split Number into Date and Time using C# and VB.Net

Last Reply 3 months ago By pandeyism

Posted 3 months ago

I want this number 201812140444 in  one textbox in gridview like 

14-12-2018 04:044 AM.

 

Please guide

                    string datetimeValue = lines[i].Split(' ')[j].Substring(0, 1);
                    DateTime Date = new DateTime(Convert.ToInt32(datetimeValue.Substring(0, 4)), Convert.ToInt32(datetimeValue.Substring(4, 2)), Convert.ToInt32(datetimeValue.Substring(6, 2)));
                    string time = lines[i].Split(' ')[j].Substring(8, 4);
                    TimeSpan Time = new TimeSpan(Convert.ToInt32(time.Substring(0, 2)), Convert.ToInt32(time.Substring(2, 2)), 0);

 

You are viewing reply posted by: pandeyism 3 months ago.
Posted 3 months ago

Hey akhter,

Please refer below sample.

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    string str = "201812140444";
    DateTime date = Convert.ToDateTime(str.Insert(4, "-").Insert(7, "-").Insert(10, " ").Insert(13, ":"));
    Response.Write(date.ToString("dd-MM-yyyy hh:mm:tt"));
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim str As String = "201812140444"
    Dim datee As DateTime = Convert.ToDateTime(str.Insert(4, "-").Insert(7, "-").Insert(10, " ").Insert(13, ":"))
    Response.Write(datee.ToString("dd-MM-yyyy hh:mm:tt"))
End Sub

Output

14-12-2018 04:44:AM