C# Get/Retrieve Week In Month And Week In Year

0 comments



*Note get DayOfWeek, Week In Month, Week In Year, Today Date, Today Month, Today Year, Today Time And Format.

Using 

using System.Globalization;

Function


        public static int Iso8601WeekNumber(DateTime dt)
        {
            System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
            int weekNumOnMonth = ci.Calendar.GetWeekOfYear(dt, ci.DateTimeFormat.CalendarWeekRule, ci.DateTimeFormat.FirstDayOfWeek);
            return weekNumOnMonth;
        }

        public static int GetWeekInMonth(DateTime date)
        {
            DateTime tempdate = date.AddDays(-date.Day + 1);
            CultureInfo ciCurr = CultureInfo.CurrentCulture;
            int weekNumStart = ciCurr.Calendar.GetWeekOfYear(tempdate, CalendarWeekRule.FirstFourDayWeek, ciCurr.DateTimeFormat.FirstDayOfWeek);
            int weekNum = ciCurr.Calendar.GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek, ciCurr.DateTimeFormat.FirstDayOfWeek);
            return weekNum - weekNumStart + 1;
        }

Eg.


WeekInYear = Iso8601WeekNumber(DateTime.Now); //This Year Week
WeekInMonth = GetWeekInMonth(DateTime.Now); //This Month Week
DayOfWeek = DateTime.Now.DayOfWeek.ToString(); //Day Of Week eg.Monday
MonthOnly = DateTime.Now.Month; //Today Month
DateOnly = DateTime.Now.Day; //Today Day
YearOnly = DateTime.Now.Year; //Today Year
TimeOnly2400 = DateTime.Now.ToString("HH:mm:ss tt"); //Now Time In 2400 Format
TimeOnly1200 = DateTime.Now.ToString("h:m tt"); //Now Time In 1200 Format

C# Retrieve/Get Last Character

0 comments



Way to retrieve last character of the string.

public string GetLast(string s, int tail_length)
{
            if (tail_length >= s.Length)
                return s;
            return s.Substring(s.Length - tail_length);
}

eg.


string OneYearLastChar = GetLast(System.DateTime.Now.Year.ToString(), 1);
- Get Last Character

string TwoLastChar = GetLast(System.DateTime.Now.Year.ToString(), 2);
- Get Last Two Character

C# Request.QueryString/ Get Parameter Value From URL

0 comments


This request query string act as action to get url parameter.

eg. 

Request.QueryString["id"]
url: http://Sample.com&id=12345
Request.QueryString will get the value of 12345

How do we get array parameter from URL using Request.QueryString.

eg.

string[] names = Request.QueryString.GetValues("id");
names will hold the value of the parameter pass through URL.

This is the way to retrieve or get value from parameter.

Closing Webpage Without Prompt

0 comments


Add Javascript to webpage


    function AssignContent()
    {
        window.open('', '_self', '');
        window.close();
    }

Add C# code at webpage aspx page.


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!ClientScript.IsClientScriptBlockRegistered("k1"))
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "k1", "<script language='javascript'>AssignContent();</script>");
                }
            }
        }

During form onload the webpage will close.