# Days has increment to 2.. RSS

## 4 replies

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

Member

6 Points

22 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

Contributor

2042 Points

345 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
• Edited by alankarp on Dec 07, 2012 03:48 AM
• Edited by alankarp on Dec 07, 2012 03:49 AM
• Edited by alankarp on Dec 07, 2012 03:50 AM

Star

7983 Points

2099 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

6 Points

22 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

11739 Points

1097 Posts

Microsoft

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

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

Please mark the replies as answers if they help or unmark if not.
Feedback to us

Develop and promote your apps in Windows Store