How can I separate date and time separately?
You can get the column datetime
as an object datetime
and then format it twice - once with a format that ignores the time portion, and once with a custom format that ignores the date portion.
var d = new DateTime(2014,12,26,17,21,30);
Console.WriteLine("Date: {0:d/M/yyyy} Time: {0:hh:mm:ss}", d);
or
var d = new DateTime(2014,12,26,17,21,30);
var dtPart = d.ToShortDateString();
var tmPart = d.ToShortTimeString();
source to share
The format of my datetime in db is 2014-12-24 17:23:35
The time in the database has no format. It is stored in its binary form. If you need to display, as expected, no updates than using the same database column twice while using two string formats at the same time, one for the date only and one for the time part only, like in .Net, you can use "d "and" t ".
Code for you:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
<!DOCTYPE html>
<%
var dateTimeColumnFromDatabase = DateTime.Now;
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<p>Date: <%= dateTimeColumnFromDatabase.ToString("d") %></p>
<p>Time: <%= dateTimeColumnFromDatabase.ToString("t") %></p>
</body>
</html>
Of course you need to connect your database code instead DateTime.Now
If you are using any third party controls, they usually have a Format property where you can specify the required formatting to display.
source to share
I found an easy way to do this if you still have problems.
result= "7/26/2017 12:00:00 AM"; Whatever your variable is
string[] Separate = result.Split (' ');
string desiredDate = Separate [0];
Debug.Log (desiredDate);
If you need time, just create a variable with the second element of the array.
source to share
DateTime dt = DateTime.Now; //Gets the current date
string datetime = dt.ToString(); //converts the datetime value to string
string[] DateTime = datetime.Split(' '); //splitting the date from time with the help of space delimeter
string Date = DateTime[0]; //saving the date value from the string array
string Time = DateTime[1]; //saving the time value
**NOTE:** The value of the index position will vary depending on how the DateTime
value is stored on your server or in your database if you're fetching from one.
Output: 10/16/2019 1:38:24 PM
10/16/2019 1:38:24 PM
["10/16/2019","1:38:24","PM"]
10/16/2019
1:38:24
source to share