Ruslan Popescu
Ruslan Popescu

Reputation: 55

How to get hours only from a timespan

How do I use only hours from DateTime.Now.ToString() for setting a value (eg: a price) for two or more intervals?

I want to set a variable p differently for every 5 hours.

Upvotes: 2

Views: 2332

Answers (2)

user1475623
user1475623

Reputation:

Here is how you covert to string. If you want both methods, either work. https://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx

System.DateTime currentTime = System.DateTime.Now
int t = currentTime.Hour 
string s = convert.toString(t)
s

This is another way of doing it as well

string time = currentTime.ToShortTimeString()

Upvotes: 1

smead
smead

Reputation: 1808

int p = DateTime.Now.Hour;

MSDN Documentation

Upvotes: 1

Related Questions