Monday, August 31, 2015

C# web window date calculator birtday check, date month year, two date differece checker


Get days difference between two given date. Total days



 ----.cs file code on button click
 protected void btndateDiff_Click(object sender, EventArgs e)
    {
        DateTime dtFrom = Convert.ToDateTime(txtFromDate.Text);
        DateTime dtTo = Convert.ToDateTime(txtToDate.Text);
        string res = (dtTo - dtFrom).TotalDays.ToString();
        lblMsg.Text = "<br /> Total Days = " + res;
        Age objA = new Age(dtFrom, dtTo);  /////////////age class
        lblMsg.Text += "<br /> Total--> Year: " + objA.Years + "/Months: " + objA.Months + "/Days: " + objA.Days;       
    }

/////////////age class

#region age calculation method
public class Age
{
    public int Years;
    public int Months;
    public int Days;

    public Age(DateTime Bday)
    {
        this.Count(Bday);
    }

    public Age(DateTime Bday, DateTime Cday)
    {
        this.Count(Bday, Cday);
    }

    public Age Count(DateTime Bday)
    {
        return this.Count(Bday, DateTime.Today);
    }

    public Age Count(DateTime Bday, DateTime Cday)
    {
        if ((Cday.Year - Bday.Year) > 0 ||
            (((Cday.Year - Bday.Year) == 0) && ((Bday.Month < Cday.Month) ||
              ((Bday.Month == Cday.Month) && (Bday.Day <= Cday.Day)))))
        {
            int DaysInBdayMonth = DateTime.DaysInMonth(Bday.Year, Bday.Month);
            int DaysRemain = Cday.Day + (DaysInBdayMonth - Bday.Day);

            if (Cday.Month > Bday.Month)
            {
                this.Years = Cday.Year - Bday.Year;
                this.Months = Cday.Month - (Bday.Month + 1) + Math.Abs(DaysRemain / DaysInBdayMonth);
                this.Days = (DaysRemain % DaysInBdayMonth + DaysInBdayMonth) % DaysInBdayMonth;
            }
            else if (Cday.Month == Bday.Month)
            {
                if (Cday.Day >= Bday.Day)
                {
                    this.Years = Cday.Year - Bday.Year;
                    this.Months = 0;
                    this.Days = Cday.Day - Bday.Day;
                }
                else
                {
                    this.Years = (Cday.Year - 1) - Bday.Year;
                    this.Months = 11;
                    this.Days = DateTime.DaysInMonth(Bday.Year, Bday.Month) - (Bday.Day - Cday.Day);
                }
            }
            else
            {
                this.Years = (Cday.Year - 1) - Bday.Year;
                this.Months = Cday.Month + (11 - Bday.Month) + Math.Abs(DaysRemain / DaysInBdayMonth);
                this.Days = (DaysRemain % DaysInBdayMonth + DaysInBdayMonth) % DaysInBdayMonth;
            }
        }
        else
        {
            throw new ArgumentException("Birthday date must be earlier than current date");
        }
        return this;
    }
}
#endregion

Asp code-------------------------
--------------------------------date calculation-------------
<br />
        From : <asp:TextBox ID="txtFromDate" runat="server"></asp:TextBox>[yyyy/mm/dd]<br />
        To : <asp:TextBox ID="txtToDate" runat="server"></asp:TextBox>[yyyy/mm/dd]<br />
        <asp:Button ID="btndateDiff" runat="server" Text="Diff" OnClick="btndateDiff_Click" /><br />

Add Sharepoint Visual visual webpart in Master Page and Page Layout without extra html tag or Without sharepoint tag


Jquery Calendar and Nice popup after click submit button or modern calender c# website and sharepoint site, visual webpart

Attractive jquery poup with good theme like :
On the click of OK button It can also redirect to  another page.


Download link of css and js
Code in design of page(aspx/ascx) :
<script src="../_layouts/15/FolderName/popup/jquery.min.js"></script>
<script src="../_layouts/15/FolderName/popup/Popup1.js"></script>
<link href="../_layouts/15/FolderName/popup/popupman.css" rel="stylesheet" />


1.   For POpup(  Without redirection on another page)
<script type="text/javascript">
    function ShowPopup1(message) {
        $(function () {
            $("#dialog").html(message);
            $("#dialog").dialog({
                title: "Message",
                buttons: {
                    Close: function () {
                        $(this).dialog('close');
                    }
                },
                modal: true
            });
        });
    }

</script>
2.  TO REDIRECT ON ANOTHER PAGE

<script type="text/javascript">
function ShowPopup1(message) {
           $(function () {
               $("#dialog").html(message);
               $("#dialog").dialog({
                   title: "Message",
                   buttons: {
                       OK: function () {
                           $(this).dialog('OK');
                           var url = '/Pages/Home.aspx';
                           $(location).attr('href', url);
                       }
                   },
                   modal: true
               });
           });
       }
</script>

Div to show popup
<%--popup div--%>
<div id="dialog" style="display: none">
</div>


C# code to call popup:
string message = "Saved Successfully.";
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup1('" + message + "');", true); 


2. FOR CALENDAR SAME CSS AND JAVASCRIPT
 
<script type="text/javascript">
    $(function () {

        $(window).bind('mousewheel DOMMouseScroll', function (event) {
            if ($("#ui-datepicker-div").css("display") === "block") {
                $(".hasDatepicker").datepicker("hide");
                $(".hasDatepicker").blur();
            }
        });


        $('#<%= txtFlightDate.ClientID %>').datepicker({

            buttonImage: "calendar.png",
            buttonImageOnly: true,

            changeYear: true,
            changeMonth: true,
            yearRange: "2000:2025",
            autoSize: true,
            dateFormat: "yy-mm-dd"
        });
    });


</script>