 We are excited to announce that the ASP.NET Forums are moving to the new Microsoft Q&A experience. Learn more >

## 4 replies

Last post Jun 27, 2009 01:54 PM by anders__f

• ### calculate values of days

sorry my english isn't good, but I'll try to explain
How can I calculate the values in a date

for example

date = #25/07/2009#
i wanted
results = 2+5+0+7+2+0+0+9 = 25

• ### Re: calculate values of days

Pass the date to this function:

```Public Function SumOfIntegersInDate(ByVal anyDate As Date) As Integer
Dim sum As Integer = 0

Dim iDay As Integer = Day(anyDate)
Dim iMonth As Integer = Month(anyDate)
Dim iYear As Integer = Year(anyDate)

Dim dayOnes As Integer = iDay Mod 10
Dim dayTens As Integer = iDay \ 10

Dim monthOnes As Integer = iMonth Mod 10
Dim monthTens As Integer = iMonth \ 10

Dim yearOnes As Integer = iYear Mod 10
Dim yearTens As Integer = (iYear \ 10) Mod 10
Dim yearHundreds As Integer = (iYear \ 100) Mod 10
Dim yearThousands As Integer = iYear \ 1000

Return sum
End Function```

That should take care of it.

• ### Re: calculate values of days

This is one way of doing it. You need to have "using System.Linq;" in the beginnging of your code file.

I can explain what happens in the code if you don't get it.

```public void AnyMethod()
{
var sum = GetSum(DateTime.Now);
}

private int GetSum(DateTime date)
{
return date
.ToShortDateString()
.Where(IsDigit)
.Select(c => int.Parse(c.ToString()))
.Sum();
}

private bool IsDigit(char c)
{
return c >= '0' && c <= '9';
}```
• ### Re: calculate values of days

its work, and i understood
Argument not specified for parameter 'c' of 'Private Function IsDigit(c As Char) As Boolean'.

Return [date].ToShortDateString().Where(IsDigit).[Select](Function(c) Integer.Parse(c.ToString())).Sum()

Private Function IsDigit(ByVal c As Char) As Boolean
Return (c >= "0"c AndAlso c <= "9"c)
End Function

• ### Re: calculate values of days

I'm sorry for answering in C# when you posted in the Visual Basic.NET forum. My mistake.

However it seems like you somehow managed to convert the code to VB. I have never used VB myself so I can't comment on that, but the error you are getting may come from a C# syntax shortcut I used in my example.

These two methods are equivalent:

```private int GetSum(DateTime date)
{
return date
.ToShortDateString()
.Where(IsDigit)
.Select(c => int.Parse(c.ToString()))
.Sum();
}```

```private int GetSum(DateTime date)
{
return date
.ToShortDateString()
.Where(c => IsDigit(c))
.Select(c => int.Parse(c.ToString()))
.Sum();
}```