简体   繁体   English

从对象调用函数

[英]Calling a function from object

I retrieve date by calling 我通过致电检索日期

event[0].getDateCreated() // output in GMT

but I want it to Local 但我要本地

I do not want to convert to local as 我不想转换为本地

Utilities.formatDate(event[0].getDateCreated(), tz, "E, dd MMMM YYYY hh:mm a")

instead - I want to convert by using a function attached to date object 相反-我想通过使用附加到date对象的函数进行转换

as

event[0].getDateCreated().toLcl()

should accomplish the above task. 应该完成以上任务。

How do I define a toLcl() 如何定义toLcl()

I tried with 我尝试过

function toLcl()
{
    return Utilities.formatDate(this, tz, "E, dd MMMM YYYY hh:mm a")
}

Use JavaScript Date prototype Property 使用JavaScript日期原型属性

The prototype constructor allows you to add new properties and methods to the Date() object. 原型构造函数使您可以向Date()对象添加新的属性和方法。

Date.prototype.myMethod = function() {
    this.localDate = Utilities.formatDate(this, tz, "E, dd MMMM YYYY hh:mm a");

};

Use it like this : 像这样使用它:

var d = new Date();
d.myMethod();
var localDate = d.localDate;

Hope this helps ! 希望这可以帮助 !

You can add a property in the prototype of the Date . 您可以在Dateprototype中添加一个属性。 Do not alter the prototype directly. 不要直接更改prototype Use Object#defineProperty instead: 使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM