[英]Calling a function from object
我通过致电检索日期
event[0].getDateCreated() // output in GMT
但我要本地
我不想转换为本地
Utilities.formatDate(event[0].getDateCreated(), tz, "E, dd MMMM YYYY hh:mm a")
相反-我想通过使用附加到date对象的函数进行转换
如
event[0].getDateCreated().toLcl()
应该完成以上任务。
如何定义toLcl()
我尝试过
function toLcl()
{
return Utilities.formatDate(this, tz, "E, dd MMMM YYYY hh:mm a")
}
使用JavaScript日期原型属性
原型构造函数使您可以向Date()对象添加新的属性和方法。
Date.prototype.myMethod = function() {
this.localDate = Utilities.formatDate(this, tz, "E, dd MMMM YYYY hh:mm a");
};
像这样使用它:
var d = new Date();
d.myMethod();
var localDate = d.localDate;
希望这可以帮助 !
您可以在Date
的prototype
中添加一个属性。 不要直接更改prototype
。 使用Object#defineProperty
代替:
Object.defineProperty(Date.prototype, 'locale', {
get: () => Utilities.formatDate(this, tz, "E, dd MMMM YYYY hh:mm a");
});
const myDate = new Date();
const localeDate = myDate.locale
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.