c# - Calculating Day, Month, Year -


how calculate day, month, year exactly?

means..

from 2th jan 1990 9th may 2009 is..

xxx days, xxx months, xxx years.

any idea how that?

i tried timespan , tick().. both failed so..

you cannot through direct calculation (i.e. there's no "totalmonths" or "totalyears" property of timespan, because numbers not make sense arbitrary intervals of time).

instead, can count number in loop, so:

var dt1 = new datetime(1990, 1, 2); var dt2 = new datetime(2009, 5, 9);  int years = 0; while (dt1.addyears(1) < dt2) {     years ++;     dt1 = dt1.addyears(1); }  int months = 0; while (dt1.addmonths(1) < dt2) {     months ++;     dt1 = dt1.addmonths(1); }  int days = (int) math.floor(dt2.subtract(dt1).totaldays); 

i haven't tested this, there might off-by-one errors or whatever, that's basic idea.


Comments

Popular posts from this blog

unicode - Are email addresses allowed to contain non-alphanumeric characters? -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

c++ - Convert big endian to little endian when reading from a binary file -