我需要将日期对象转换为 SQL Server 能够理解的字符串。这是我目前所拥有的:
(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();
}
我得到的错误是: 对象 Tue Jan 29 ...(东部标准时间)没有方法 MMDDYYYY。
这可能很明显,但我不明白如何制作原型(prototype)。 使用 jQuery 是可以接受的。
最佳答案
我认为问题不在于作用域,而在于如何创建 objDate!我已经做了几次尝试,当 objDate 是一个字符串时出现了这个错误。所以,这段代码按预期工作,问题是传递给 DateTimeFormat 的参数:我认为它是这样创建的 objDate = Date()
而不是 objDate = new Date()
。后者将正确地创建 Date 对象,而不是以 String 格式返回日期。
关于javascript - 将日期对象转换为日期字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14588158/