-1

I've been stuck on this for awhile, I'm not even sure how to begin it because I'm a total noob with C#. At first I was utilizing DateTime.Now in order to get my local time, and tried to use that with TimeSpan in order to get how many hours are left in the day.

I've deleted that and completely started over and have hit a dead end, what do you recommend I do?

Habib
  • 219,104
  • 29
  • 407
  • 436
user3708761
  • 275
  • 1
  • 6
  • 14
  • 1
    Can you show us what you did try and how _exactly_ it did not work? It's easier to help you when you show your attempt., – gunr2171 Aug 26 '14 at 19:03

2 Answers2

3

Try this...

int hours = (DateTime.Today.AddDays(1) - DateTime.Now).Hours;

DateTime.Today.AddDays(1) gives you next date and DateTime.Now will give you system time. Now (DateTime.Today.AddDays(1) - DateTime.Now) will return total time stamp. Just call its Hours property and you will get hours left in day.

Dinesh Maind
  • 349
  • 1
  • 7
  • Can you expand your answer to include an explanation of your code? It helps the reader more than you might think. – gunr2171 Aug 26 '14 at 19:10
  • DateTime.Today.AddDays(1) gives you next date and DateTime.Now will give you system time. Now (DateTime.Today.AddDays(1) - DateTime.Now) will return total time stamp. Just call its Hours property and you will get hours left in day. – Dinesh Maind Aug 26 '14 at 19:14
  • Please edit your post to include that information. – gunr2171 Aug 26 '14 at 19:14
  • Thank you very much, I dont know how I managed to hurt my head with this. I need to really look into how methods work here. – user3708761 Aug 26 '14 at 19:22
1

Subtract mid night time (DateTime) with current time and you can access TimeSpan.TotalHours like:

DateTime currentTime = DateTime.Now;
DateTime midNightToday = DateTime.Today.AddDays(1).AddTicks(-1);
var hourDifference = (midNightToday - currentTime).TotalHours;
Habib
  • 219,104
  • 29
  • 407
  • 436
  • 1
    I feel like an absolute idiot right now, thank you very much for your assistance. Edit: I guess hitting enter doesn't actually go down. Anyway, what I was originally doing was just... I was definitely going out of my way, thank you. – user3708761 Aug 26 '14 at 19:06
  • There's no need to subtract a tick here. – Matt Johnson-Pint Aug 26 '14 at 21:45