## 4 replies

Last post Dec 11, 2012 12:26 AM by Amy Peng - MSFT

Member

2 Points

19 Posts

### Days has increment to 2..

my days has the problem..since i enter 1 year 3months and 1 day...it should be 456..instead of 1day it gives me 3 days,equal to  458days..same as if i enter only for months and days or include all(year,months, days,) it gives the wrong result,however years and days it gives correct output as days not increment to 2..i prefer the result for days not work perfectly

eg

wrong result in bold

enter 3 years 2days = 3 year 2 days

3months 2 days = 3months 4days

1 year 3months and 1 day = 1 year 3 months 3days

```Public Function RaceDate(length  As Integer ) As String

dim year As string=0
dim month As string=0
dim day As string=0
dim ReturnString As string

ReturnString=""

If length = 0 then
Return String.Empty
end if

if length >= 365
year=cstr((Math.Floor (length / 365)))
length  =(length  Mod 365)

end if

If length >30 AndAlso length < 366 Then
month =cstr((Math.Floor(length / 30)))
length  =(length  Mod 30)

end if

if length<30 Then
day =cstr(length)
End If

If cint(year) >0
ReturnString= year+"  Years  "

End if

If cint(month) >0
ReturnString= ReturnString +month+" Months "

End if

If cint(day) >0
ReturnString=ReturnString+day+" Days"

End if

Return(ReturnString)

End Function```

thanks

Participant

1341 Points

335 Posts

### Re: Days has increment to 2..

Can you specify what exactly you want, your explanation suggest you want to convert days to year,month and days but your code suggest opposite. I think issue is you are assuming days of month as 30. Please see below code, am using different approach

```Dim days__1 As Integer
days__1 = 456
Dim Age As DateTime = DateTime.MinValue.AddDays(days__1)
Dim Years As Integer = Age.Year - 1
Dim Months As Integer = Age.Month - 1
Dim Days__2 As Integer = Age.Day - 1
Console.WriteLine(Years)
Console.WriteLine(Months)
Console.WriteLine(Days__2)```

Thanks

Alanakr

PS: I am C# programmer so I converted c# code to vb by using online convertor

Alankar Patil

Profile

Contributor

4554 Points

1770 Posts

### Re: Days has increment to 2..

Hai

Like this way

```var dueDate = new DateTime(2011, 3, 3);

//if you want to increment six days

//if you want to increment six months

var daysDiff1 = (dueDatePlus6Days - dueDate).TotalDays; //gives 6
var daysDiff2 = (dueDatePlus6Months - dueDate).TotalDays; //gives 184```

Member

2 Points

19 Posts

### Re: Days has increment to 2..

thanks alankarp..

my days should all be 30 days no matter if its leap year..my code above looks ok ..but it happens if i enter 1 month 1days it gives me 1 month 2days instead of 1..if i have 1 year 1 days..it dispaly the coreect output which 1 year 1 days..i prefer it happens between months and days..

thanks

Star

14544 Points

1481 Posts

Microsoft

### Re: Days has increment to 2..

Dec 11, 2012 12:26 AM|Amy Peng - MSFT|LINK

I just convert your code to C#, it can work very well, please try to refer to:

```public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

Response.Write(RaceDate(400)); // it can return 1 Years 1 Months 5 Days
}

public string RaceDate(int length)
{

string year ="0";
string month = "0";
string day = "0";
string ReturnString = null;

ReturnString = "";

if (length == 0) {
return string.Empty;
}

if (length >= 365) {
year = Convert.ToString((length / 365));
length = (length % 365);

}

if (length > 30 && length < 366) {
month = Convert.ToString(((length / 30)));
length = (length % 30);

}

if (length < 30) {
day = Convert.ToString(length);
}

if (Convert.ToInt32(year) > 0) {
ReturnString = year + "  Years  ";

}

if (Convert.ToInt32(month) > 0) {
ReturnString = ReturnString + month + " Months ";

}

if (Convert.ToInt32(day) > 0) {
ReturnString = ReturnString + day + " Days";

}

return (ReturnString);

}

}```

Thanks,
Amy Peng

We are trying to better understand customer views on social support experience. Click HERE to participate the survey. Thanks!