I am using google API to create some events in google calendar. I need to get the date and time from the user input. This might be my logic issue but I can't figure it out. Here is my code:
var dateTime;
app.post('/', function (req, res) {
res.render('index');
var date = req.body.date; //I get date from user
var time = req.body.time; // I get time from user
dateTime = date +'T'+ time+":00"; //trying to create the RFC3339 fromat
console.log("date time: ",dateTime);
createEvents(jwtClient);
});
var event = {
'start': {
'dateTime': dateTime,
'timeZone': 'America/Los_Angeles',
},
'end': {
'dateTime': '2020-06-12T12:00:00',
'timeZone': 'America/Los_Angeles',
},
'conferenceData': {
'createRequest': {
'conferenceSolutionKey': {
'type': 'hangoutsMeet'
},
'requestId': 'iyfuted65e3ers'
}
},
'attendees': [
{'email': 'test@test.com'},
{'email': 'jhfk@test.com'},
],
'reminders': {
'useDefault': false,
'overrides': [
{'method': 'email', 'minutes': 24 * 60},
{'method': 'popup', 'minutes': 10},
],
},
};
//Create Appointment
function createEvents(auth) {
const calendar = google.calendar({ version: 'v3', auth });
calendar.events.insert({
calendarId: 'primary',
conferenceDataVersion: 1,
resource: event,
}, (err, res) => {
if (err) return console.log('The API returned an error: ' + err);
else {
result = res.data.hangoutLink;
console.log(result," start date: ", event.start.dateTime);
}
});
}
when I print the dateTime in my post section it is the right date that user entered but it does not recognize it in event variable. and I get this error message:
Error: Start and end times must either both be date or both be dateTime.
I also have another question, how can I add an hour to start time and use it for end time?
You can see the text fields that user can enter the date and time here
UPDATE:
The RFC3339 formatted string expects the time in the format hh:mm:ss
.
Sample how to convert time object in the format hh-mm
into an RFC3339 formatted string:
var date = req.body.date; //I get date from user
var time = req.body.time; // I get time from user
time = time.split("-")[0] + ":" + time.split("-")[0];
var dateTime = date +'T'+ time+ ":00";
I did not quite understand how you want to process the user time zone, but here are two samples showing what is possible:
var now = new Date();
Logger.log("Current time:" + now);
var dateTime = new Date(now.getTime()).toISOString().split(".")[0];
Logger.log(dateTime);
var event = {
'start': {
'dateTime': dateTime,
'timeZone': 'America/Los_Angeles',
},
'end': {
'dateTime': '2020-06-12T12:00:00',
'timeZone': 'America/Los_Angeles',
},
...
var now = new Date();
Logger.log("Current time:" + now);
var offset = new Date().getTimezoneOffset();
offset = (offset / (-60));
if (offset < 10) {
offset = '0'+offset;
}
offset = '+' + offset + ':00';
var dateTime = new Date(now.getTime()-new Date().getTimezoneOffset()*60*1000).toISOString().split(".")[0] + offset;
Logger.log(dateTime);
var event = {
'start': {
'dateTime': dateTime
},
'end': {
'dateTime': '2020-06-12T12:00:00-07:00'
},
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.