tags:

views:

16712

answers:

12

C#: How do you get the current time (not date AND time)?

Example: 5:42:12 PM

+33  A: 

DateTime.Now.TimeOfDay gives it to you as a TimeSpan (from midnight).

DateTime.Now.ToString("HH:mm:ss tt") gives it to you as a string.

Mark Brackett
DateTime.Now.ToShortTimeString() does the same as the second suggestion.
Kyle Trauberman
well, almost the same, it returns a string but is missing the seconds portion of the time.
Kyle Trauberman
+3  A: 

Get the current date and time, then just use the time portion of it. Look at the possibilities for formatting a date time string in the MSDN docs.

Greg D
+2  A: 

datetime.TimeOfDay returns a TimeSpan and might be what you are looking for.

Stefan Schultze
+2  A: 

DateTime.Now.TimeOfDay

or

DateTime.Now.ToShortTimeString()

benPearce
A: 

Get the current date and time, then just use the time portion of it.

The Date is the time. Internally things get counted as 'number of seconds since some point in time' which I think is Midnight, 1 January, 1600, or something like that.

Orion Edwards
A: 

Current time with AM/PM designator:

DateTime.Now.ToString("hh:mm:ss tt", System.Globalization.DateTimeFormatInfo.InvariantInfo)
DateTime.Now.ToString("hh:mm:ss.fff tt", System.Globalization.DateTimeFormatInfo.InvariantInfo)

Current time using 0-23 hour notation:

DateTime.Now.ToString("HH:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo)
DateTime.Now.ToString("HH:mm:ss.fff", System.Globalization.DateTimeFormatInfo.InvariantInfo)
Oppositional
A: 

This will show you only the current time, in 24 hour format:

class Program
{
 static void Main(string[] args)
 {
  Console.WriteLine(DateTime.Now.ToLongTimeString().ToString());
  Console.WriteLine(DateTime.Now.ToShortTimeString().ToString());
  Console.ReadLine();
 }
}

Regards
K

Khb
+1  A: 

Another option using String.Format()

string.Format("{0:HH:mm:ss tt}", DateTime.Now)
cxfx
A: 

By the way, I want the same data but in DateTime type, cause this is all for strings right?

Angel Escobedo
You should do a new question and link to this one as a reference. The short answer though is that all DateTimes must have a date and a time.
John Sheehan
A: 

Hi guys,

Try this one. Its working for me in 3tier Architecture Web Application. :) (But No use here)

'" + DateTime.Now.ToString() + "'

Please remember the Single Quotes in the insert Query.

For example see my example Insertion.

    string Command = @"Insert Into CONFIG_USERS(smallint_empID,smallint_userID,str_username,str_pwd,str_secquestion,str_secanswer,tinyint_roleID,str_phone,str_email,Dt_createdOn,Dt_modifiedOn) values (" + u.Employees + "," + u.UserID + ",'" + u.Username + "','" + u.GetPassword() + "','" + u.SecQ + "','" + u.SecA + "'," + u.RoleID + ",'" + u.Phone + "','" + u.Email + "','" + DateTime.Now.ToString() + "','" + DateTime.Now.ToString() + "')";

The DateTime insertion at the end of the line.

Let me know how it is working in your Web apps... ?

Regards

Albert A Albs

A: 

How can we get the datetime from internet using a webservice. Does anyone know of any such webservice?

A: 

Excuse me but what type of namespace should I pick to have the compiler recognize "Datetime" and deal with that.

I have tested out : Using System.Datetime; It's not working