为什么Google日历API比使用node.js给予的日期/时间早4小时插入我的事件?

发布于 2025-01-28 01:09:05 字数 2350 浏览 3 评论 0原文

我终于有了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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉世弥音 2025-02-04 01:09:05

一个原因之一可能是.00z appdate变量末尾的零件,因为它涉及utc时区(z表示UTC或Zulu)时间)。尝试通过使用格式-04:00(加拿大/东部)设置所需时区。

示例

const apptDate = new Date(`${date}T${time}:00-04:00`).toISOString()

更新

在再次查看问题后,我认为API调整了您在apptdate actibal中设置的日期和时间(.000Z,UTC), 时区选项。在这种情况下,它是加拿大/东部,UTC-4小时,因此它比您设置的工作删除了4小时。

datetime文档说

时间,作为组合日期时间值(根据
RFC3339)。除非时区是
在时区中明确指定。

因此,一种清洁方法是使用apptdate(UTC),而没有timezone选项。

示例

const apptDate = new Date(`${date}T${time}:00.000Z`).toISOString()

...

'start': {
        'dateTime': `${apptDate}`
    },
    'end': {
        'dateTime': `${endDateTime}`
    }

更多信息

calendar api documentation

One reason could be the .00Z part at the end of your appDate variable as it refers to the UTC 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

const apptDate = new Date(`${date}T${time}:00-04:00`).toISOString()

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 the timeZone option. In this case, it's Canada/Eastern, UTC-4 hours, so it removed 4 hours from what you have set.

The dateTime documentation says

The time, as a combined date-time value (formatted according to
RFC3339). A time zone offset is required unless a time zone is
explicitly specified in timeZone.

So a cleaner approach would be to use the apptDate as it is (UTC), without the timeZone option.

Example

const apptDate = new Date(`${date}T${time}:00.000Z`).toISOString()

...

'start': {
        'dateTime': `${apptDate}`
    },
    'end': {
        'dateTime': `${endDateTime}`
    }

More information

Calendar API documentation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文