Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

Find the first date of a week from a given date In C#

using System;
using System.Globalization;

namespace FirstDateOfWeek
{
    class Program
    {
        static void Main(string[] args)
        {
            int yr, mn, dt;
            Console.Write("

 Find the first day of a week against a given date :
");
            Console.Write("--------------------------------------------------------
");

            Console.Write(" Enter the Day : ");
            dt = Convert.ToInt32(Console.ReadLine());
            Console.Write(" Enter the Month : ");
            mn = Convert.ToInt32(Console.ReadLine());
            Console.Write(" Enter the Year : ");
            yr = Convert.ToInt32(Console.ReadLine());
            DateTime d = new DateTime(yr, mn, dt);
            Console.WriteLine(" The formatted Date is : {0}", d.ToString("dd/MM/yyyy"));
            var culture = CultureInfo.CurrentCulture;
            var diff = d.DayOfWeek - culture.DateTimeFormat.FirstDayOfWeek;
            if (diff < 0)
                diff += 7;
            d = d.AddDays(-diff).Date;
            Console.WriteLine(" The first day of the week for the above date is : {0}
", d.ToString("dd/MM/yyyy"));
            Console.ReadKey();
        }
    }
}
Source by www.yogeshhadiya.in #
 
PREVIOUS NEXT
Tagged: #Find #date #week #date #In
ADD COMMENT
Topic
Name
3+1 =