Javascript 如何更改google日历事件请求返回的日期格式?
我是API的新手,我正在尝试将其应用于实践。我正在尝试将来自事件请求的数据格式化到我的谷歌日历中。它目前正在以ISO格式返回,但我希望它是一个更基本的日期格式(最好只是事件的时间范围) 以下是函数:Javascript 如何更改google日历事件请求返回的日期格式?,javascript,google-api,google-calendar-api,Javascript,Google Api,Google Calendar Api,我是API的新手,我正在尝试将其应用于实践。我正在尝试将来自事件请求的数据格式化到我的谷歌日历中。它目前正在以ISO格式返回,但我希望它是一个更基本的日期格式(最好只是事件的时间范围) 以下是函数: function listUpcomingEvents() { var request = gapi.client.calendar.events.list({ 'calendarId': 'primary', 'timeMin': (new
function listUpcomingEvents() {
var request = gapi.client.calendar.events.list({
'calendarId': 'primary',
'timeMin': (new Date()).toISOString(),
'showDeleted': false,
'singleEvents': true,
'maxResults': 10,
'orderBy': 'startTime'
});
When I try to alter the toISOString method to a toDateSring(), the information does not display at all.
以下是完整的代码:
<html>
<head>
<script type="text/javascript">
// Your Client ID can be retrieved from your project in the Google
// Developer Console, https://console.developers.google.com
var CLIENT_ID = 'INSERT_YOUR_CLIENT_ID';
var SCOPES = ["https://www.googleapis.com/auth/calendar.readonly"];
/**
* Check if current user has authorized this application.
*/
function checkAuth() {
gapi.auth.authorize(
{
'client_id': CLIENT_ID,
'scope': SCOPES.join(' '),
'immediate': true
}, handleAuthResult);
}
/**
* Handle response from authorization server.
*
* @param {Object} authResult Authorization result.
*/
function handleAuthResult(authResult) {
var authorizeDiv = document.getElementById('authorize-div');
if (authResult && !authResult.error) {
// Hide auth UI, then load client library.
authorizeDiv.style.display = 'none';
loadCalendarApi();
} else {
// Show auth UI, allowing the user to initiate authorization by
// clicking authorize button.
authorizeDiv.style.display = 'inline';
}
}
/**
* Initiate auth flow in response to user clicking authorize button.
*
* @param {Event} event Button click event.
*/
function handleAuthClick(event) {
gapi.auth.authorize(
{client_id: CLIENT_ID, scope: SCOPES, immediate: false},
handleAuthResult);
return false;
}
/**
* Load Google Calendar client library. List upcoming events
* once client library is loaded.
*/
function loadCalendarApi() {
gapi.client.load('calendar', 'v3', listUpcomingEvents);
}
/**
* Print the summary and start datetime/date of the next ten events in
* the authorized user's calendar. If no events are found an
* appropriate message is printed.
*/
function listUpcomingEvents() {
var request = gapi.client.calendar.events.list({
'calendarId': 'primary',
'timeMin': (new Date()).toISOString(),
'showDeleted': false,
'singleEvents': true,
'maxResults': 10,
'orderBy': 'startTime'
});
request.execute(function(resp) {
var events = resp.items;
appendPre('Upcoming events:');
if (events.length > 0) {
for (i = 0; i < events.length; i++) {
var event = events[i];
var when = event.start.dateTime;
if (!when) {
when = event.start.date;
}
appendPre(event.summary + ' (' + when + ')')
}
} else {
appendPre('No upcoming events found.');
}
});
}
/**
* Append a pre element to the body containing the given message
* as its text node.
*
* @param {string} message Text to be placed in pre element.
*/
function appendPre(message) {
var pre = document.getElementById('output');
var textContent = document.createTextNode(message + '\n');
pre.appendChild(textContent);
}
</script>
<script src="https://apis.google.com/js/client.js?onload=checkAuth">
</script>
</head>
<body>
<div id="authorize-div" style="display: none">
<span>Authorize access to Google Calendar API</span>
<!--Button for the user to click to initiate auth sequence -->
<button id="authorize-button" onclick="handleAuthClick(event)">
Authorize
</button>
</div>
<pre id="output"></pre>
</body>
</html>
//您的客户ID可以从Google中的项目中检索
//开发者控制台,https://console.developers.google.com
var CLIENT_ID='插入您的客户_ID';
变量作用域=[”https://www.googleapis.com/auth/calendar.readonly"];
/**
*检查当前用户是否已授权此应用程序。
*/
函数checkAuth(){
gapi.auth.authorize(
{
“客户id”:客户id,
“scope”:SCOPES.join(“”),
“立即”:真
},handleAuthResult);
}
/**
*处理来自授权服务器的响应。
*
*@param{Object}authResult授权结果。
*/
函数handleAuthResult(authResult){
var authorizeDiv=document.getElementById('authorized-div');
if(authResult&!authResult.error){
//隐藏身份验证UI,然后加载客户端库。
authorizeDiv.style.display='none';
loadCalendarApi();
}否则{
//显示授权UI,允许用户通过
//单击“授权”按钮。
authorizeDiv.style.display='inline';
}
}
/**
*响应用户单击“授权”按钮,启动身份验证流。
*
*@param{Event}Event按钮单击事件。
*/
函数handleAuthClick(事件){
gapi.auth.authorize(
{client_id:client_id,scope:SCOPES,immediate:false},
手工(结果);
返回false;
}
/**
*加载谷歌日历客户端库。列出即将发生的事件
*一旦客户端库被加载。
*/
函数loadCalendarApi(){
加载('calendar','v3',listUpcomingEvents);
}
/**
*打印中接下来十个事件的摘要和开始日期时间/日期
*授权用户的日历。如果未找到任何事件,则
*适当的信息被打印出来。
*/
函数listUpcomingEvents(){
var请求=gapi.client.calendar.events.list({
'日历ID':'主要',
'timeMin':(新日期()).toISOString(),
“showDeleted”:false,
“singleEvents”:对,
“maxResults”:10,
“订购人”:“开始时间”
});
请求执行(功能(resp){
风险值事件=相应项目;
appendPre(“即将到来的事件:”);
如果(events.length>0){
对于(i=0;i
我是否应该在请求后格式化数据?请原谅我的任何公然无知。我对google API及其许多概念都很陌生。是的,您应该在
请求中操作日期。执行回调函数。您可以查看包含所有相关信息的event
变量,可能还有日期。实际上,您可以在appendPre(event.summary+'('+when+')行前面操作when
变量。