Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format date as weeknumber

Status
Not open for further replies.

GoTerps88

Programmer
Apr 30, 2007
174
US
Does such a function exist?

I was thinking "{0:ww}" or something but that doesn't work.

Thanks...
 
As far as I am aware there is nothing like that? I think you have to do it the old fashion way
Code:
/// <summary>
/// find week number
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static int FindWeekNumber( DateTime searchDate )
{		
    const double weekDay = 7.0;
    int weekNum = 1;

    DateTime newWeek = new DateTime( DateTime.Now.Year, 1, 1 );
    for ( int i = 0; i < 365; i++ )
    {
	newWeek = newWeek.AddDays( weekDay );
	if ( newWeek > searchDate )
	   break;
	else
           weekNum++;
    }
    return weekNum;
 }

this code needs to go backwards to the begining of the week after the loop just to check if the week is this week?

Age is a consequence of experience
 
whoops, 6 needs to be added to i in the loop :(

Age is a consequence of experience
 
there are 1,710,000 hits for week number c# in google so there must be an easier way than iterating through every day of the year. ISO 8601 looks pretty damn complicated, too.


mr s. <;)

 
The work around I created was using a calculated field from the database that formatted the weeknumber, then create a bound column in the gridview based on that.

I'm assuming that if I created my own weeknumber function, I would have to implement that in the RowDataBound event.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top