Click here to Skip to main content
13,191,642 members (71,022 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hi,

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

Kindly anyone please hep me!

Thanks
Posted 12-Jul-11 0:41am
Updated 16-Dec-16 5:07am
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.

See http://www.csharp-examples.net/string-format-datetime/
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  
v2
Comments
SAKryukov 12-Jul-11 13:00pm
   
No, there is no Time property -- hence the question.
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
ahsan22e 12-Jul-11 6:53am
   
yes i want to ignore date!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

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)
 
        MessageBox.Show(OnlyHour.ToString)
    End Sub
End Class


Result show only time component:
"01/01/0001 08:53:40 a.m."
  Permalink  
v2
Comments
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".
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

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(' ');
Console.WriteLine(sDate[1]);


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;
Console.WriteLine(stime);
Console.ReadLine();
 
  Permalink  
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web01 | 2.8.171017.2 | Last Updated 16 Dec 2016
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100