Click here to Skip to main content
15,349,004 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

I have a problem, i want to use Time part from my Date Time SQL Column.

Kindly anyone please hep me!

Updated 16-Dec-16 5:07am

Probably the easiest (but wont win any code beauty contests) is to go through the DateTime value in your code and strip away the date part.

Your specs are not 100% clear, but the C# DateTime object has a Date property and a Time property. If you can use the datetime object in SQL Server as your column type, just read it in the DateTime object and get it out throught the Time property or the ToString method eg. (if text is sufficient).

if your SQL Server datatype can contain time only I'm pretty sure it will accept the DateTime.Time property object when you pass it as a parameter.

Hope this helps.
Sergey Alexandrovich Kryukov 12-Jul-11 13:00pm
No, there is no Time property -- hence the question.
Your code makes no sense. Does Rows.Add even return something ? What do you want to add ? You can use DatePart in the DB to get only the time. Or just use the time part in the object that is returned. There is no Time object in C#. I doubt there is one in SQL. So what do you mean by 'use time part'. Do you want to ignore the date in a query ? use DatePart to compare times only.
Ahsan Mirza 12-Jul-11 6:53am
yes i want to ignore date!
new Date(Date.Now.Ticks - Date.Now.Date.Ticks)

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim SampleDate As Date = Date.Now

        'Substract entire date to the date component of SampleDate
        Dim OnlyHour As Date = New Date(SampleDate.Ticks - SampleDate.Date.Ticks)

    End Sub
End Class

Result show only time component:
"01/01/0001 08:53:40 a.m."
CHill60 3-Apr-14 14:46pm
Apart from the fact your answer is incomplete it's over 2 years late
eyanson 3-Apr-14 17:48pm
The answer is correct. It works and works well. If you will not serve you will serve another.
CHill60 3-Apr-14 18:58pm
I have no idea what "If you will not serve you will serve another" means. However, giving the OP an answer in multiples of 100 nanoseconds without any further explanation is not really helpful... especially as the question is over 2 years old. The key phrases I have used here are "over 2 years old" and "not really helpful".
Hi, There is no Time object in C#.
But there is an alternate way to fetch the time
through the split method.

So, for that first you have to convert the value
in string and then split that value to get the time value.
DateTime dDate = DateTime.Now;
string[] sDate = dDate.ToString().Split(' ');

In above code you will simply get the time but if you want to have time with AM and PM as well then you can concatenate the string as follows
DateTime dDate = DateTime.Now;
string[] sDate = dDate.ToString().Split(' ');

string stime = sDate[1];
string sampm = sDate[2];
stime = stime + " " + sampm;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900