vim
vim

Reputation: 874

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.

My datetime format in db is 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#?

Upvotes: 3

Views: 62697

Answers (7)

Ken Gordon
Ken Gordon

Reputation: 305

I found a simple way of doing it, if you are still having trouble.

    var 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.

even you can make your custom format

    var result= "7/26/2017 12:00:00 AM"; // Whatever your variable is
    string[] Separate = result.Split (' ');
    string desiredDate = Separate [0];
    Debug.Log (desiredDate); // OUTPUT: 7/26/2017

    string[] SeparateDate = result.Split ('/');
    Debug.Log (SeparateDate[0]); // OUTPUT: 7
    Debug.Log (SeparateDate[1]); // OUTPUT: 26
    Debug.Log (SeparateDate[2]); // OUTPUT: 2017

    // for OUTPUT: 26/7/2017
    Debug.Log (SeparateDate[1]+"/"+SeparateDate[0]+"/"+SeparateDate[2]); 

Upvotes: 2

Tahir77667
Tahir77667

Reputation: 2512

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

Upvotes: 0

farrukh aziz
farrukh aziz

Reputation: 172

DateTime Date = DateTime.Parse(txtDate.Text).ToShortDateString(); // For Date
DateTime Date = DateTime.Parse(txtDate.Text).ToShortTimeString(); // for time

Upvotes: 0

Tien Dinh
Tien Dinh

Reputation: 1038

DateTime dtValue;  // load your date & time into this variable
TextBox1.Text = dtValue.ToString("yyyy-MM-dd");
TextBox2.Text = dtValue.ToString("HH:mm:ss");

Upvotes: 12

Darek
Darek

Reputation: 4797

My datetime format in db is 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".

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

If you use any 3rd party controls, they usually have Format property where you can specify the needed formatting for display.

Upvotes: 1

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137408

If you already have a DateTime object, this is simple:

  • The Date property returns just the date part
  • The TimeOfDay property returns just the time part

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726589

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.

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();

Upvotes: 9

Related Questions