Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# show only Time portion of DateTime

Tags:

c#

I have a date that shows up as 10/18/2011 3:12:33 PM

How do I get only the time portion of this datetime?

I am using C#.

I tried:

      string timeval = PgTime.ToShortTimeString();

but that did not work as Intellisense only showed ToString();

like image 719
Nate Pet Avatar asked Jan 03 '12 20:01

Nate Pet


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


1 Answers

Assuming that

DateTime PgTime;

You can:

String timeOnly = PgTime.ToString("t");

Other format options can be viewed on MSDN.

Also, if you'd like to combine it in a larger string, you can do either:

// Instruct String.Format to parse it as time format using `{0:t}`
String.Format("The time is: {0:t}", PgTime);

// pass it an already-formatted string
String.Format("The time is: {0}", PgTime.ToString("t"));

If PgTime is a TimeSpan, you have a few other options:

TimeSpan PgTime;

String formattedTime = PgTime.ToString("c"); // 00:00:00 [TimeSpan.ToString()]
String formattedTime = PgTime.ToString("g"); // 0:00:00
String formattedTime = PgTime.ToString("G"); // 0:00:00:00.0000000
like image 72
Brad Christie Avatar answered Sep 28 '22 14:09

Brad Christie