简体   繁体   English

如何将日期和时间分开?

[英]How can I split date and time separate?

I have a datetime column in mysql db column and I need to show it in separate date and time textboxes.我在 mysql db 列中有一个 datetime 列,我需要在单独的日期和时间文本框中显示它。

My datetime format in db is 2014-12-24 17:23:35 .我在 db 中的日期时间格式是2014-12-24 17:23:35 And I want in:我想要:

Date : 2014-12-24  
Time : 17:23:35

How can I do it in c#?我怎样才能在 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 retrieve your datetime column as a DateTime object, and then format it twice - once with a format that ignores the time part, and once with a custom format that ignores the date part.您可以将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);

Demo.演示。

or或者

var d = new DateTime(2014,12,26,17,21,30);
var dtPart = d.ToShortDateString();
var tmPart = d.ToShortTimeString();

My datetime format in db is 2014-12-24 17:23:35我在 db 中的日期时间格式是 2014-12-24 17:23:35

Datetime in a database does not have a format.数据库中的日期时间没有格式。 It is stored in its native binary form.它以其本机二进制形式存储。 If you only need to display, as in no updates expected, than use the same database column twice , while leveraging two string formats, one for just date part, and one for only time part, for example in .Net apps you can use "d" and "t".如果您只需要显示,因为没有预期的更新,那么使用相同的数据库列两次,同时利用两种字符串格式,一种仅用于日期部分,一种仅用于时间部分,例如在 .Net 应用程序中,您可以使用“ d”和“t”。

Some 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 have to plug in your database code in place of DateTime.Now当然,您必须插入数据库代码来代替DateTime.Now

If you use any 3rd party controls, they usually have Format property where you can specify the needed formatting for display.如果您使用任何 3rd 方控件,它们通常具有 Format 属性,您可以在其中指定显示所需的格式。

If you already have a DateTime object, this is simple:如果您已经有一个DateTime对象,这很简单:

  • The Date property returns just the date part Date属性只返回日期部分
  • The TimeOfDay property returns just the time part TimeOfDay属性只返回时间部分

I found a simple way of doing it, if you are still having trouble.如果您仍然遇到问题,我找到了一种简单的方法。

    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 the time, just create a variable with the second array element.如果您需要时间,只需使用第二个数组元素创建一个变量。

DateTime Date = DateTime.Parse(txtDate.Text).ToShortDateString(); // For Date
DateTime Date = DateTime.Parse(txtDate.Text).ToShortTimeString(); // for time
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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM