-2

Rent calculating for each day from check-in time and checkout time. If check-in at 31-08-2016 and checkout at 01-09-2016 then it was calculating rent to 30 days. But it has to calculate for only 1 day.

char sp = '/';
string[] date = checkin.Split(sp);
string[] date2 = checkout.Split(sp);
int c1 = Convert.ToInt32(date[0]);
int c0 = Convert.ToInt32(date2[0]);
totday = c0 - c1;
H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
satish
  • 1
  • 4
  • 2
    what exactly is your question? – Raktim Biswas Aug 20 '16 at 12:09
  • Here is a clue - don't store dates in strings use the correct datatype. Start googling c# datatypes – Mike Aug 20 '16 at 12:10
  • Please state you question more clearly and think about values of intermediate variables in your code. [DateTime stuct](https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx) might be helpful for you - [here you have some more details how to use it](http://stackoverflow.com/questions/1607336/calculate-difference-between-two-dates-number-of-days). – Tomasz Maczyński Aug 20 '16 at 12:14
  • Please state your problem more clearly. – Ralf Bönning Aug 20 '16 at 12:35
  • See this _[fiddle](https://dotnetfiddle.net/0Wy7RI)_ if this is what you were looking for. – Raktim Biswas Aug 20 '16 at 12:41
  • Judging you code, I'd say it calculates for minus 30 days even – Pleun Aug 20 '16 at 13:07

1 Answers1

2

Honestly I'm not quite sure what is being asked here, but I'd recommend using the correct types. Here is a code snippet you may be asking for?

using System;

public class Program
{
    public static void Main()
    {

        DateTime checkIn = new DateTime(2016, 8, 31);
        DateTime checkOut = new DateTime(2016, 9, 1);

        TimeSpan difference = checkOut - checkIn;

        Console.WriteLine(difference);

    }
}
briandoesdev
  • 141
  • 6
  • 1
    Yep. Do the arithmetic with `DateTime` objects. @satish, read up on properties and methods - these make arithmetic *easy* IF YOU KNOW THE SECRET: **Key to understanding DateTime:** Every DateTime always has the "fully qualified value" consisting of year, month, day, hour, minutes, seconds, milliseconds. If you don't want the *time*, for example, to factor into any calculations make sure time is set to zero. The `Date` property returns the datetime object with hours, minutes, seconds, milliseconds all zeroed. – radarbob Aug 20 '16 at 18:32