locked
convert date to dd/MM/YYYY format in ajax RRS feed

  • Question

  • User-1722422105 posted

    Hi 

    below My code in ajax shows

    if (!String.prototype.parseNETDate) {
    String.prototype.parseNETDate = function () {
    if (this.indexOf('-') == -1) {
    return new Date(parseInt(this.slice(6, 19)));
    } else {
    return new Date(parseInt(this.slice(6, 20)));
    }
    };
    }

    out out is  Tue Oct 10 1989 00:00:00 GMT+0530 (India Standard Time)

    i want op in dd/MM/YYYY 

    Wednesday, June 20, 2018 3:37 AM

All replies

  • User-1716253493 posted

    AFAIK, formated date is a string

    string datestr = thedate.ToString("dd/MM/yyyy");

    Wednesday, June 20, 2018 12:10 PM
  • User-330142929 posted

    Hi Sidu,

    According to your description, I think you convert string to data by adding a string prototype function.

    I suggest that you could achieve the function you want by adding the following function to the Date prototype.

    I have made a demo, wish it is useful to you.

        <script>
            Date.prototype.Format = function (fmt) { //author: meizz   
                    var o = {
                        "M+": this.getMonth() + 1,                 //MM   
                        "d+": this.getDate(),                    //dd 
                        "h+": this.getHours(),                   //hh   
                        "m+": this.getMinutes(),                 //mm   
                        "s+": this.getSeconds(),                 //ss   
                        "q+": Math.floor((this.getMonth() + 3) / 3), //qq   
                        "S": this.getMilliseconds()             //S 
                    };
                    if (/(y+)/.test(fmt))
                        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
                    for (var k in o)
                        if (new RegExp("(" + k + ")").test(fmt))
                            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                    return fmt;
                }
                var d1=new Date(2018,03,22,12,34,28).Format("dd/MM/yyyy");
                var d2=new Date(2018,03,22,12,34,28).Format("dd/MM/yyyy hh:mm:ss");
                alert(d1+"\n"+d2);
        </script>

    How It works.

    Please feel free to let me know, if you have any question.

    Best Regards,

    Abraham.

    Thursday, June 21, 2018 6:17 AM