JavaScript  

Display Server-Side Date in Alertify Alert Using JavaScript

To implement a backend solution where the date is stored in a hidden field, formatted, and then displayed via alertify, you need to take the following steps:

  1. Server-side (Backend): Set the date value in the hidden field, ensuring it's a proper Date format (e.g., yyyy-MM-dd or a JavaScript-friendly string).

  2. Client-side (JavaScript): Retrieve that value, format it to the desired format, and then show it in an alertify alert.

1. Server-Side: Set the Date in the Hidden Field

For the server-side, let's assume you're using ASP.NET or any server-side language to inject the date into the hidden field. Here’s an example in ASP.NET:

ASP.NET (C#) Example

In your ASP.NET code (C#), you might set the hidden field like this:

<!-- In your ASP.NET markup -->
<input type="hidden" id="issuefromdatehdn" runat="server" value="<%= DateTime.Now.ToString("yyyy-MM-dd") %>" />
(or)
<asp:Hidden  id="issuefromdatehdn" runat="server">

Backend Data Bind :
issuefromdatehdn.Value=ds.Tables[0].Rows[0]["Startdate"].ToString();

This sets the value of the hidden field to the current date in the format yyyy-MM-dd (e.g., 2025-11-05).

2. Client-Side JavaScript: Format the Date and Display in Alertify

Next, in your JavaScript, you will read that hidden field's value, format it to DD-MMM-YYYY, and display it in the alertify alert.

Updated JavaScript Example

function Upcomingoffline() {
    // Get the date string from the hidden field
    var issueDateString = document.getElementById('issuefromdatehdn').value;

    // Create a Date object from the string
    var date = new Date(issueDateString); 

    // Array of month names for formatted month
    var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    
    // Format the date to "DD-MMM-YYYY"
    var formattedDate = ("0" + date.getDate()).slice(-2) + "-" + months[date.getMonth()] + "-" + date.getFullYear();

    // Show the alert with the formatted date
    alertify.alert("Your Application is processed. You will receive mandate on " + formattedDate + ".", function (e) {
        if (e) {
            window.history.pushState(null, "", window.location.href);
            window.location = '/test.aspx';
        } else {
            window.location = '/test2.aspx';
        }
    });
}

Explanation

  1. Retrieve Hidden Field Value

    • The document.getElementById('issuefromdatehdn').value retrieves the date value from the hidden input field. This value is expected to be in the yyyy-MM-dd format (or whatever format is provided by the server-side code).

  2. Formatting the Date

    • The new Date(issueDateString) creates a Date object from the string.

    • We extract the day, month, and year, then reformat the date as DD-MMM-YYYY (e.g., 05-Nov-2025).

  3. Displaying the Date in Alertify

    • The formatted date is included in the alertify.alert message.

Putting It All Together

  • The hidden field issuefromdatehdn is populated on the server-side, where the date is formatted (in yyyy-MM-dd format).

  • When the JavaScript function Upcomingoffline() runs, it retrieves the value, formats the date, and then shows the formatted date in the alertify message.