Google uses AI technology to translate content into your preferred language. AI translations can contain errors.
Class DateField
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
日期字段
已弃用。请改用 People API 高级服务
联系人中的日期字段。
此类仅供“通讯录”服务使用,Apps Script 中其他位置使用的日期使用 JavaScript 的标准
Date 对象。
详细文档
getLabel()
获取此字段的标签。这可以是 Field、ExtendedField 或 String。
// Logs the label for all the address fields associated with contact
// 'John Doe'. This method can be similarly called for any field that has
// a label.
const contacts = ContactsApp.getContactsByName('John Doe');
const addressFields = contacts[0].getAddresses();
for (let i = 0; i < addressFields.length; i++) {
Logger.log(addressFields[i].getLabel());
}
返回
Object - 此字段的标签
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.google.com/m8/feeds
setLabel(label)
设置此字段的标签。
// Sets the label to 'Apartment' for the first address field associated
// with contact 'John Doe'. This method can be similarly called for any
// field that has a label.
const contacts = ContactsApp.getContactsByName('John Doe');
const addressFields = contacts[0].getAddresses();
addressFields[0].setLabel('Apartment');
参数
| 名称 | 类型 | 说明 |
label | String | 此字段的新标签 |
返回
DateField - this 字段,适用于链式调用
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.google.com/m8/feeds
已弃用的方法
deleteDateField()
已弃用。此函数已废弃,不应在新脚本中使用。
删除此日期。
// Deletes all the dates that are set for contact 'John Doe'
const contacts = ContactsApp.getContactsByName('John Doe');
const dates = contacts[0].getDates();
for (let i = 0; i < dates.length; i++) {
dates[i].deleteDateField();
}
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.google.com/m8/feeds
getDay()
已弃用。此函数已废弃,不应在新脚本中使用。
获取此日期所在的一月中的第几天。
注意:对于标准 JavaScript Date 对象,
getDay() 方法会改为返回一周中的第几天。
// Logs the day of the birthday for contact 'John Doe'
const contacts = ContactsApp.getContactsByName('John Doe');
const birthday = contacts[0].getDates(ContactsApp.Field.BIRTHDAY)[0];
Logger.log(birthday.getDay());
返回
Integer - 日期
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.google.com/m8/feeds
getMonth()
已弃用。此函数已废弃,不应在新脚本中使用。
获取此日期所在的月份。
// Logs the month of the birthday for contact 'John Doe'
const contacts = ContactsApp.getContactsByName('John Doe');
const birthday = contacts[0].getDates(ContactsApp.Field.BIRTHDAY)[0];
Logger.log(birthday.getMonth());
返回
Month - 月份
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.google.com/m8/feeds
getYear()
已弃用。此函数已废弃,不应在新脚本中使用。
获取此日期的年份。
// Logs the year of the birthday for contact 'John Doe'
const contacts = ContactsApp.getContactsByName('John Doe');
const birthday = contacts[0].getDates(ContactsApp.Field.BIRTHDAY)[0];
Logger.log(birthday.getYear());
返回
Integer - 年份
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.google.com/m8/feeds
setDate(month, day)
已弃用。此函数已废弃,不应在新脚本中使用。
将日期设置为今天,不指定年份。
此方法仅适用于不需要年份的日期字段,例如生日。
// Sets the birthday for contact 'John Doe' to April 1
const contacts = ContactsApp.getContactsByName('John Doe');
const birthday = contacts[0].getDates(ContactsApp.Field.BIRTHDAY)[0];
birthday.setDate(ContactsApp.Month.APRIL, 1);
参数
| 名称 | 类型 | 说明 |
month | Month | 月份 |
day | Integer | 这一天 |
返回
DateField - 此日期,适用于链式调用
抛出
Error - 如果此日期字段需要年份
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.google.com/m8/feeds
setDate(month, day, year)
已弃用。此函数已废弃,不应在新脚本中使用。
将日期设置为今天。
// Sets the birthday for contact 'John Doe' to April 1, 1980
const contacts = ContactsApp.getContactsByName('John Doe');
const birthday = contacts[0].getDates(ContactsApp.Field.BIRTHDAY)[0];
birthday.setDate(ContactsApp.Month.APRIL, 1, 1980);
参数
| 名称 | 类型 | 说明 |
month | Month | 月份 |
day | Integer | 这一天 |
year | Integer | 年 |
返回
DateField - 此日期,适用于链式调用
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.google.com/m8/feeds
setLabel(label)
已弃用。此函数已废弃,不应在新脚本中使用。
设置此字段的标签,例如“生日”或“周年纪念”。
// Retrieves the first date that's set for contact 'John Doe' and re-labels
// it as an anniversary
const contacts = ContactsApp.getContactsByName('John Doe');
const firstDate = contacts[0].getDates()[0];
firstDate.setLabel(ContactsApp.Field.ANNIVERSARY);
参数
返回
DateField - this 字段,适用于链式调用
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.google.com/m8/feeds
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-12-11。
[null,null,["最后更新时间 (UTC):2025-12-11。"],[],[]]