Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a date object into a date string

I need to convert a date object into a string that SQL Server will understand. This is what I have so far:

(function() {
    Date.prototype.MMDDYYYY = function() {
        var month, day, year;
        month = String(this.getMonth() + 1);
        if (month.length === 1) {
            month = "0" + month;
        }
        day = String(this.getDate());
        if (day.length === 1) {
            day = "0" + day;
        }
        year = String(this.getFullYear());
        return month + '/' + day + '/' + year;
    }
})();

(function() {
    Date.prototype.HHMMSS = function() {
        var hour, minute, second;
        hour = String(this.getHours());
        minute = String(this.getMinutes());
        second = String(this.getSeconds());
        return '' + hour + ':' + minute + ':' + second;
    }
})();


function DateTimeFormat(objDate) {
    return objDate.MMDDYYYY() + ' ' + objDate.HHMMSS();
}

The error I'm getting is: Object Tue Jan 29 ... (Eastern Standard Time) has no method MMDDYYYY.

It might be obvious, but I don't understand how to prototype. Using jQuery is acceptable.

like image 205
Phillip Senn Avatar asked Nov 21 '25 19:11

Phillip Senn


1 Answers

I think the problem isn't scope, but how do you create the objDate! I've done a few tries, and that error come up when objDate is a String. So, this snippet of code works as expected, the problem is the parameter passed to DateTimeFormat: I think it'is created like this objDate = Date() instead of objDate = new Date(). The latter will correctly create a Date object instead of returning date in a String format.

like image 191
Iazel Avatar answered Nov 23 '25 08:11

Iazel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!