为什么Google日历API比使用node.js给予的日期/时间早4小时插入我的事件?
我终于有了Google日历API将事件插入我的日历中,但是该事件比我给出的开始时间早4小时插入。
这是我为插入它所做的。在此示例中: 日期变量为“ 2022-05-18”,并且 时间变量是'17:18',并且 然后,当我委托apptdate变量时,输出为2022-05-18T17:18:00.000Z,
但事件已插入我的日历中的2022-05-18,在13:18。
export const insertGoogleCalendarEvent = async (req, res)=> {
try{
const {firstName, lastName, date, time, duration} = req.body
const apptDate = new Date(`${date}T${time}:00.000Z`).toISOString()
const SCOPES = ['https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/calendar.events']
const GOOGLE_PRIVATE_KEY = process.env.GOOGLE_PRIVATE_KEY
const GOOGLE_CLIENT_EMAIL = process.env.GOOGLE_CLIENT_EMAIL
const GOOGLE_PROJECT_NUMBER = process.env.GOOGLE_PROJECT_NUMBER
const GOOGLE_CALENDAR_ID = process.env.GOOGLE_CALENDAR_ID
let endDateTime = ''
if (duration === '60') {
let newDate = new Date(apptDate)
newDate.setHours(newDate.getHours() + 1)
endDateTime = newDate.toISOString()
} else if (duration === '75') {
let newDate = new Date(apptDate)
newDate.setMinutes(newDate.getMinutes() + 75)
endDateTime = newDate.toISOString()
} else if (duration === '90') {
let newDate = new Date(apptDate)
newDate.setMinutes(newDate.getMinutes() + 90)
endDateTime = newDate.toISOString()
}
const event = {
'summary': `${firstName} ${lastName}`,
'start': {
'dateTime': `${apptDate}`,
'timeZone': 'Canada/Eastern'
},
'end': {
'dateTime': `${endDateTime}`,
'timeZone': 'Canada/Eastern'
}
}
const jwtClient = new google.auth.JWT(
GOOGLE_CLIENT_EMAIL,
null,
GOOGLE_PRIVATE_KEY,
SCOPES
)
const calendar = google.calendar({
version: 'v3',
project: GOOGLE_PROJECT_NUMBER,
auth: jwtClient
})
calendar.events.insert({
calendarId: GOOGLE_CALENDAR_ID,
resource: event,
}, {
function (err, event) {
if (err) {
console.log('this is an error', err)
return
}
console.log('Event created:', event.htmlLink);
}
})
} catch {
console.log('error')
}
}
I have finally got the google calendar api to insert events into my calendar with Node.js, but the event is inserted 4 hours earlier than the start time that I've given it.
Here's what I've done to get it to insert. In this example:
the date variable is '2022-05-18' and
the time variable is '17:18' and
then when I console.log the apptDate variable, the output is 2022-05-18T17:18:00.000Z
but the event is inserted in my calendar on 2022-05-18 at 13:18.
export const insertGoogleCalendarEvent = async (req, res)=> {
try{
const {firstName, lastName, date, time, duration} = req.body
const apptDate = new Date(`${date}T${time}:00.000Z`).toISOString()
const SCOPES = ['https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/calendar.events']
const GOOGLE_PRIVATE_KEY = process.env.GOOGLE_PRIVATE_KEY
const GOOGLE_CLIENT_EMAIL = process.env.GOOGLE_CLIENT_EMAIL
const GOOGLE_PROJECT_NUMBER = process.env.GOOGLE_PROJECT_NUMBER
const GOOGLE_CALENDAR_ID = process.env.GOOGLE_CALENDAR_ID
let endDateTime = ''
if (duration === '60') {
let newDate = new Date(apptDate)
newDate.setHours(newDate.getHours() + 1)
endDateTime = newDate.toISOString()
} else if (duration === '75') {
let newDate = new Date(apptDate)
newDate.setMinutes(newDate.getMinutes() + 75)
endDateTime = newDate.toISOString()
} else if (duration === '90') {
let newDate = new Date(apptDate)
newDate.setMinutes(newDate.getMinutes() + 90)
endDateTime = newDate.toISOString()
}
const event = {
'summary': `${firstName} ${lastName}`,
'start': {
'dateTime': `${apptDate}`,
'timeZone': 'Canada/Eastern'
},
'end': {
'dateTime': `${endDateTime}`,
'timeZone': 'Canada/Eastern'
}
}
const jwtClient = new google.auth.JWT(
GOOGLE_CLIENT_EMAIL,
null,
GOOGLE_PRIVATE_KEY,
SCOPES
)
const calendar = google.calendar({
version: 'v3',
project: GOOGLE_PROJECT_NUMBER,
auth: jwtClient
})
calendar.events.insert({
calendarId: GOOGLE_CALENDAR_ID,
resource: event,
}, {
function (err, event) {
if (err) {
console.log('this is an error', err)
return
}
console.log('Event created:', event.htmlLink);
}
})
} catch {
console.log('error')
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个原因之一可能是
.00z
appdate
变量末尾的零件,因为它涉及utc
时区(z表示UTC或Zulu)时间)。尝试通过使用格式-04:00
(加拿大/东部)设置所需时区。示例
更新
在再次查看问题后,我认为API调整了您在
apptdate
actibal中设置的日期和时间(.000Z
,UTC),时区
选项。在这种情况下,它是加拿大/东部
,UTC-4小时,因此它比您设置的工作删除了4小时。datetime
文档说因此,一种清洁方法是使用
apptdate
(UTC),而没有timezone
选项。示例
更多信息
calendar api documentation
One reason could be the
.00Z
part at the end of yourappDate
variable as it refers to theUTC
time zone (Z means UTC or Zulu time). Try to set the needed timezone by using the format-04:00
(Canada/Eastern) at the end.Example
Update
After reviewing the problem again, I think the API adjusted the date and time you have set in the
apptDate
variable (.000Z
, UTC) to the timezone you specified in thetimeZone
option. In this case, it'sCanada/Eastern
, UTC-4 hours, so it removed 4 hours from what you have set.The
dateTime
documentation saysSo a cleaner approach would be to use the
apptDate
as it is (UTC), without thetimeZone
option.Example
More information
Calendar API documentation