Mark
Mark

Reputation:

How to get the time value from DateTime datatype

I have DateTime variable, and I need to get ONLY the time value from that variable.

How can I do that in C#?

Thanks,

Upvotes: 4

Views: 7627

Answers (5)

Dimi Takis
Dimi Takis

Reputation: 4949

Alternatively you can also use the string formatting for getting the output nicely

string.Format("{0: hh:mm:ss}", DateTime.Now)

Upvotes: 4

Stanislav Kniazev
Stanislav Kniazev

Reputation: 5488

Check TimeOfDay property of the DateTime object.

Upvotes: 9

Steven
Steven

Reputation: 3928

dt.TimeOfDay will return the time as a TimeSpan object.

Upvotes: 9

Manish Basantani
Manish Basantani

Reputation: 17499

You can try this....

someDateTime.ToString("hh:mm:ss");

Upvotes: 4

Vadim
Vadim

Reputation: 21704

If you need string representation of time you can use:

dt.ToShortTimeString()
dt.ToLongTimeString()

Upvotes: 2

Related Questions