Get Week Of Month - CSharp System

CSharp examples for System:DateTime Month

Description

Get Week Of Month

Demo Code


using System.Threading.Tasks;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Linq;
using System.Collections.Generic;
using System;/*from   w  ww  .  j a  v  a 2s.co  m*/

public class Main{
        public static int GetWeekOfMonth(DateTime date)
        {
            DateTime beginningOfMonth = new DateTime(date.Year, date.Month, 1);
            while (date.Date.AddDays(1).DayOfWeek != System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek)
                date = date.AddDays(1);
            return (int)Math.Truncate((double)date.Subtract(beginningOfMonth).TotalDays / 7f) + 1;
        }
}

Related Tutorials