C# 如何将日期和时间分开?
我在mysql db列中有一个datetime列,我需要在单独的日期和时间文本框中显示它 我的数据库日期时间格式是C# 如何将日期和时间分开?,c#,C#,我在mysql db列中有一个datetime列,我需要在单独的日期和时间文本框中显示它 我的数据库日期时间格式是2014-12-24 17:23:35。我想加入: Date : 2014-12-24 Time : 17:23:35 如何在c#?中执行此操作您可以将datetime列检索为datetime对象,然后将其格式化两次—一次使用忽略时间部分的格式,另一次使用忽略日期部分的自定义格式 var d = new DateTime(2014,12,26,17,21,30); Consol
2014-12-24 17:23:35
。我想加入:
Date : 2014-12-24
Time : 17:23:35
如何在c#?中执行此操作您可以将
datetime
列检索为datetime
对象,然后将其格式化两次—一次使用忽略时间部分的格式,另一次使用忽略日期部分的自定义格式
var d = new DateTime(2014,12,26,17,21,30);
Console.WriteLine("Date: {0:d/M/yyyy} Time: {0:hh:mm:ss}", d);
或
我的数据库日期时间格式是2014-12-24 17:23:35
数据库中的Datetime没有格式。它以本机二进制形式存储。如果只需要显示,如无需更新,则使用同一数据库列两次,同时利用两种字符串格式,一种仅用于日期部分,另一种仅用于时间部分,例如在.Net应用程序中,可以使用“d”和“t”
为您提供一些代码:
<%@ 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>
日期:
时间:
当然,您必须插入数据库代码来代替DateTime。现在
如果您使用任何第三方控件,它们通常具有Format属性,您可以在其中指定显示所需的格式。如果您已经有一个对象,这很简单:
- 属性只返回日期部分
- 该属性只返回时间部分
- 如果您仍然有问题,我找到了一种简单的方法
result= "7/26/2017 12:00:00 AM"; Whatever your variable is
string[] Separate = result.Split (' ');
string desiredDate = Separate [0];
Debug.Log (desiredDate);
如果您需要时间,只需使用第二个数组元素创建一个变量。您应该查看DateTime.TryParseExactIs这是WinForms、WPF、ASP.NET等吗。。。?文本框需要是可编辑的还是只读的?
数据库中的My datetime格式不以“格式”存储日期。对于任何数据库引擎来说,它只是一个数字,其格式取决于您以后如何在客户端对其进行解释@peer使用内置功能提供了一个非常好的答案代码>DateTime.ToShortTimeString()代码>它使代码更加清晰易读。@prospector已编辑。谢谢我不能在数据库中使用两列。在数据库中,它只是一列数据。我需要在asp.net页面上用c#code显示它。我没有说数据库中有两列。显示两列,这是您要求的。@Dqarek但此代码是否可以在date and time sepratley中拆分数据库datetime列?数据库datetime列包含datetime值、日期和时间的两部分。我在这里过于简单化了,因为它们本身并不是部分。在显示或编辑过程中,可以单独或一起对其进行寻址。如果您查看.Net文档,您会发现DateTime具有单独的日期和时间属性,但它们是DateTime相同值的一部分。有意义吗?@Jonathaan我如何在c#中将其拆分为两个不同的文本框?我们如何在日期-时间变量中加载字符串?非常感谢您的解决方案工作得非常好。这是一个绝对糟糕的想法。决不能将日期和时间值视为字符串。您将花费大量的时间格式化和解析,并且可能出现大量错误。使用控件,它是为处理日期和时间值而构建的,而不是文本框。我个人不使用文本框来接收日期和时间输入,但是使用文本框作为日期和时间的只读显示是非常好的。这种方法很短、简单且简单;)
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
DateTime Date = DateTime.Parse(txtDate.Text).ToShortDateString(); // For Date
DateTime Date = DateTime.Parse(txtDate.Text).ToShortTimeString(); // for time
result= "7/26/2017 12:00:00 AM"; Whatever your variable is
string[] Separate = result.Split (' ');
string desiredDate = Separate [0];
Debug.Log (desiredDate);
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