Monday, June 13, 2022

Age Calculator Javascript

Javascript Code:     

                        let EPOCH = new Date(0);

                        let EPOCH_YEAR = EPOCH.getUTCFullYear();

                        let EPOCH_MONTH = EPOCH.getUTCMonth();

                        let EPOCH_DAY = EPOCH.getUTCDate();


                        var dob = document.getElementById("BirthDate").value;

                        var birthDate = new Date(dob);

                        const diff = new Date(Date.now() - birthDate.getTime());


                        var years = Math.abs(diff.getUTCFullYear() - EPOCH_YEAR);

                        var months = Math.abs(diff.getUTCMonth() - EPOCH_MONTH);

                        var days = Math.abs(diff.getUTCDate() - EPOCH_DAY);


                        $("#Result").html(years + " year(s) " + months + " month(s) " + days + " and day(s)").css("color", "red");;




HTML::: 

 <div class="col-md-3">

                <div class="form-group">

                    @Html.LabelFor(model => model.BirthDate, htmlAttributes: new { @class = "control-label", @autocomplete = "off" })<span style="color:red; font-size:15px;"> * </span>

                    <div style="margin:.1%;" id="Result"> </div>

                    @Html.EditorFor(model => model.BirthDate, new { htmlAttributes = new { @class = "form-control" } })

                    @Html.ValidationMessageFor(model => model.BirthDate, "", new { @class = "text-danger" })

                </div>

            </div>




Reference: https://dev.to/code_mystery/javascript-age-calculator-calculate-age-from-date-of-birth-o9b

No comments:

Post a Comment

Screen Record

 Windows Screen Record WindowsKey+ Alt + R Recording Starts.