需要帮助编写显示日期的代码



我有一个ajax代码用于抓取Google Calendar事件,我想动态地将两个日期放入从Google Calendar获取的URL字符串

第一个日期(timeMax=2021-02-28)应该获取当前日期并在其上添加30天第二个日期(timeMin=2021-02-15)应该只表示当前日期

我的假设是我们将创建日期变量,一个用于第一次日期(timeMax)变量,另一个用于(timeMin)变量。然后将这两个变量放入关联的URL标记中。

任何关于代码应该是什么的帮助将是伟大的。谢谢你!

//Jquery's ajax request
$.ajax({
type: 'GET',
url: 'https://www.googleapis.com/calendar/v3/calendars/eaachapter309@gmail.com/events?singleEvents=true&maxResults=30&timeMax=2021-02-28T00:00:00Z&timeMin=2021-02-15T00:00:00Z&orderBy=startTime&key=my_secret_key_goes_here',
dataType: 'json',
async: true
}).done(function(data) {
//once we get a successful response this callback function
//gets fired, and "data" contains the parsed json file .
//here we iterate over the object array
$.each(data.items, function(i, item) {
//I do this to later format these timestamps
//set options for date.toLocaleDateString() function
var options = {
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit'
};
let start = new Date(item.start.dateTime).toLocaleDateString('en-US', options);
let end = new Date(item.end.dateTime).toLocaleDateString('en-US', options);
let end_splt = end.split(',');
//append data to the list.
if (item.status != "cancelled") {
$('table.isSearch tbody').append(`<tr mbr-list mbr-list-grow="tableColumns"><td class="body-item mbr-fonts-style display-7" width=15%>${item.summary}</td><td class="body-item mbr-fonts-style display-7" width=25%>${start} - ${end_splt[1]}</td><td class="body-item mbr-fonts-style display-7" width=60%>Location: ${item.location}<br><br>${item.description}</td></tr>`);
}
});
}).fail(function(e) {
error(e);
});
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

谢谢Mark,这确实与其他一些代码调整工作。感谢提供参考URL。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
//function myDateFunction() {

// Variable for Date Max in URL string
var dateMax = new Date(); // Now
dateMax.setDate(dateMax.getDate() + 30) // Set now + 30 days as the new date
var dateStringMax = dateMax.toISOString().split('T')[0]
// Variable for Date Min in URL string
var dateMin = new Date(); // Now
dateMin.setDate(dateMin.getDate()) // Set now + 30 days as the new date
var dateStringMin = dateMin.toISOString().split('T')[0]
// Set variable for url
var urlCalendar = 'www.googleapis.com/calendar/v3/calendars/eaachapter309@gmail.com/events?singleEvents=true&maxResults=30&timeMax=ABC123T00:00:00Z&timeMin=XYZ123T00:00:00Z&orderBy=startTime&key=my_secret_key_goes_here';
urlCalendar = urlCalendar.replace("ABC123", dateStringMax);
urlCalendar = urlCalendar.replace("XYZ123", dateStringMin);
urlCalendar = 'https://' + urlCalendar;
//alert(urlCalendar); //Use for testing to ensure dynamic URL is generated
//document.write(urlCalendar)
//}
</script>
<script>
//Jquery's ajax request
$.ajax({
type:'GET',
url:urlCalendar,
dataType: 'json',
async:true
}).done(function(data){
//once we get a successful response this callback function
//gets fired, and "data" contains the parsed json file .
//here we iterate over the object array
$.each(data.items, function(i, item){
//I do this to later format these timestamps
//set options for date.toLocaleDateString() function
var options = { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' };
let start = new Date(item.start.dateTime).toLocaleDateString('en-US', options);
let end = new Date(item.end.dateTime).toLocaleDateString('en-US', options);
let end_splt = end.split(',');
//append data to the list.
if(item.status != "cancelled"){
$('table.isSearch tbody').append(`<tr><td class="body-item mbr-fonts-style display-7" width=15%>${item.summary}</td><td class="body-item mbr-fonts-style display-7" width=25%>${start} - ${end_splt[1]}</td><td class="body-item mbr-fonts-style display-7" width=60%>Location: ${item.location}<br><br>${item.description}</td></tr>`);
}
});
}).fail(function(e){
error(e);
});
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
</script>

最新更新