Итак, у меня есть это задание для расчета продолжительности времени между двумя датами, и мое решение состоит в том, чтобы создать временную метку, сохраняющую продолжительность дня, месяца и года, зациклив и затем распечатав их.
Странно, хотя день никогда не бывает точным, я настраиваю его здесь и там все еще не получилось, пожалуйста, кто-нибудь объяснит мне, что не так и как я могу их исправить.
дата окончания может быть пустой, кстати C# поместит в нее 01/january/01
(если она пустая, то получит datetime.now)
public string printyear2()
{
int endday=0;
TimeSpan timeday = DateTime.Now - this._start;
DateTime end; `this is to find the save the end date`
if (this._end.Year != 1) `this one is to check if the date is datenow or date end`
{
timeday = this._end - this._start;
end = this._end;
}
else
{
endday = 1;
end = DateTime.Now;
}
int day = timeday.Days;
int days = timeday.Days;
int year = 0;
int cekyear = 0;
int month = 0;
DateTime date=new DateTime(this._start.Year,1,1);
string month1;
while (day >= 365) 'this is to calculate year'
{
date.AddYears(year);
if (date.Year % 4 == 0)
{
day -= 366;
year += 1;
}
else
{
day -= 365;
year += 1;
}
}
while (day >= 28&&date.Month!=end.Month) 'to calculate month'
{
date.AddMonths(month);
month1 = date.ToString("MMMM",CultureInfo.GetCultureInfo("id-ID")).ToLower();
if (month1=="februari") 'this is to check if this month has how many day'
{
if (date.Year % 4 == 0)
{
day -= 29;
month += 1;
}
else
{
day -= 28;
month += 1;
}
}
else if ((month1 == "april" || month1 == "juni" || month1 == "september" || month1 == "november") && day >= 29)
{
day -= 30;
month += 1;
}
else
{
month += 1;
day -= 31;
}
}
if (day < 0)
{
day = 0;
}
string kata = string.Format("{0} tahun {1} bulan {2} day {3} days", year, month, day,days);
return kata;
}