Showing posts with label DateTime. Show all posts
Showing posts with label DateTime. Show all posts

Get nth Week day of a Month

Following is the code snippet for getting the nth week day of any Month.
public DateTime GetNthWeekDayOfMonth(int nthWeek, int month, int year, DayOfWeek dayOfWeek)
{
    DateTime date = new DateTime(year, month, 1);
    // Find the first occurence of the weekday 
    date = date.AddDays((dayOfWeek < date.DayOfWeek ? 7 : 0) + dayOfWeek - date.DayOfWeek);
    // Find nth week day by adding the n-1 weeks 
    return date.AddDays((nthWeek - 1) * 7);
}

Efficient way to Convert Seconds in to hours,minutes and Seconds in Sql Server

Suppose you have a column contains time in seconds, and need to convert those seconds into time format (hh:MM:ss), following is the best method for doing this.


SELECT CONVERT(CHAR(8),DATEADD(SECOND,,0),108);


Ex: Suppose, you want to convert 105 seconds in to time format, then


SELECT CONVERT(CHAR(8),DATEADD(SECOND,105,0),108);


will returns

00:01:45

If you want only minutes and seconds, then you can get it with the below query


SELECT RIGHT(CONVERT(CHAR(8),DATEADD(SECOND,105,0),108),5);


will returns

01:45

It is the  fastest solution of all the other solutions.