2 replies

Last post Sep 24, 2014 01:27 PM by wavemaster

• wavemaster

Contributor

2323 Points

2254 Posts

Cannot implicitly convert type int to System.DateTime

Where is this coming from?

```int calwid = 7;
DateTime theday = DateUtils.DaysToAdd(DayOfWeek.Wednesday, DayOfWeek.Tuesday, calwid);```

```using System;
using System.Collections.Generic;
using System.Web;

public class DateUtils
{
public static int GetWeekNo(DateTime date)
{
System.Globalization.CultureInfo cult_info = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
System.Globalization.Calendar cal = cult_info.Calendar;
int weekCount = cal.GetWeekOfYear(date, cult_info.DateTimeFormat.CalendarWeekRule, cult_info.DateTimeFormat.FirstDayOfWeek);
return weekCount;
}

public static int DaysToAdd( DayOfWeek current, DayOfWeek desired, int calwid )
{
// f( c, d ) = g( c, d ) mod 7, g( c, d ) > 7
//           = g( c, d ), g( c, d ) < = 7
//   where 0 <= c < 7 and 0 <= d < 7

int c = (int)current;
int d = (int)desired;
int w = (int)calwid;
int n = (w - c + d);

return (n > 7) ? n % 7 : n;
}

public static DateTime GetNextDateForDay( DateTime startDate, DayOfWeek desiredDay, int calwid )
{
// Given a date and day of week,
// find the next date whose day of the week equals the specified day of the week.
}

}
```

• PatriceSc

All-Star

40430 Points

13169 Posts

Re: Cannot implicitly convert type int to System.DateTime

Hi,

DaysToAdd returns an int and you try to assign this integer to a DateTime... Could it be that you rather wanted to use GetNextDateForDay ???

• wavemaster

Contributor

2323 Points

2254 Posts

Re: Cannot implicitly convert type int to System.DateTime

Correct!

The call should have been made  like this:

`DateTime theday = DateUtils.GetNextDateForDay(DateTime.Today, DayOfWeek.Tuesday, calwid);`