How can I separate date and time separately?
I have a datetime column in mysql db column and I need to show it in separate datetime textboxes.
My datetime format in db is 2014-12-24 17:23:35
. I want too:
Date : 2014-12-24
Time : 17:23:35
How can I do this in C #?
DateTime dtValue; // load your date & time into this variable
TextBox1.Text = dtValue.ToString("yyyy-MM-dd");
TextBox2.Text = dtValue.ToString("HH:mm:ss");
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);
Demo version
or
var d = new DateTime(2014,12,26,17,21,30);
var dtPart = d.ToShortDateString();
var tmPart = d.ToShortTimeString();
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.
If you already have an object DateTime
, it's simple:
- The property
Date
only returns a part of the date - The property
TimeOfDay
only returns part of the time
DateTime Date = DateTime.Parse(txtDate.Text).ToShortDateString(); // For Date
DateTime Date = DateTime.Parse(txtDate.Text).ToShortTimeString(); // for time
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.
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