Wednesday 3 August 2016

Extension method to calculate First/last day of month/week in c#

 public static partial class DateTimeExtensions
    {
        public static DateTime FirstDayOfWeek(this DateTime dt)
        {
            var culture = System.Threading.Thread.CurrentThread.CurrentCulture;
            var diff = dt.DayOfWeek - culture.DateTimeFormat.FirstDayOfWeek;
            if (diff < 0)
                diff += 7;
            return dt.AddDays(-diff).Date;
        }

        public static DateTime LastDayOfWeek(this DateTime dt)
        {
            return dt.FirstDayOfWeek().AddDays(6);
        }

        public static DateTime FirstDayOfMonth(this DateTime dt)
        {
            return new DateTime(dt.Year, dt.Month, 1);
        }

        public static DateTime LastDayOfMonth(this DateTime dt)
        {
            return dt.FirstDayOfMonth().AddMonths(1).AddDays(-1);
        }

        public static DateTime FirstDayOfNextMonth(this DateTime dt)
        {
            return dt.FirstDayOfMonth().AddMonths(1);
        }
    }


After declaring the above class ,you can use it like this:

 var firstdayofThisWeek = DateTime.Now.FirstDayOfWeek();


Happy Coding!

Tuesday 26 July 2016

DataTable Issue: Cannot set property '_DT_Cellindex' of undefined



First of all check the table structure:

Number of columns in ( th ) in thead section must equal number of columns (td) in tbody section.

Happy Coding!

Wednesday 13 April 2016

Send List of string using ajax on mvc controller

  var stringArray = new Array();
// or var stringArray=[];
 stringArray.push('1');
 stringArray.push('2');

var data = JSON.stringify({
                'AllRecord': stringArray
            });

 $.ajax({
                url: 'url',
                cache: false,
                traditional: true,
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                data: data,
                dataType: "json",
                success: function (data) {
                    // Code to be on success of this call
                }
            })

//In controller grab it like below

  public ActionResult SaveCompanyAndSendMailToAllUsers(List<String> AllAgents)
        {
               // your logic
        }