如何在JavaScript中引用今天的日期? [英] How to reference today's date in JavaScript?
问题描述
我有以下代码。它是一个Apps脚本,可获取指定的两个日期之间的所有Google日历事件。
I have the following code. It is an Apps Script that gets all Google Calendar events between the two dates specified.
// from https://blog.ouseful.info/2010/03/05/grabbing-google-calendar-event-details-into-a-spreadsheet/
function caltest3(){
//http://www.google.com/google-d-s/scripts/class_calendar.html#getEvents
// The code below will retrieve events between 2 dates for the user's default calendar and
// display the events the current spreadsheet
var cal = CalendarApp.getDefaultCalendar();
var sheet = SpreadsheetApp.getActiveSheet();
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
var events = cal.getEvents(new Date("October 3, 2017 00:00:00"), new Date("October 3, 2017 23:00:00"));
for (var i=0;i<events.length;i++) {
//http://www.google.com/google-d-s/scripts/class_calendarevent.html
var details=[[events[i].getStartTime(), events[i].getEndTime(), events[i].getTitle(), events[i].getDescription()]];
var row=i+1;
var range=sheet.getRange(row,1,1,4);
range.setValues(details);
}
}
当前,这两个日期是硬编码的,声明事件
的行。我将日期范围设置为相同日期,重点是前23小时。
Currently, those two dates are hard-coded, on the line where events
is declared. I have the date range set as same-date, with a focus on the first 23 hours.
但是,由于脚本每天都会触发,因此我无法对两个日期-每个日期应该都是动态的,反映当前日期。
However, as the script will fire daily, I cannot hard-code the two dates - they should each be dynamic, reflecting the current date.
因此,如何代替硬编码日期,我如何在
So, in place of the hard-coded dates, how do I make use of the current date in the format "October 3, 2017" right there, whilst retaining the hard-coded times?
是的,我已经研究并阅读了 Date对象,但我似乎无法正确地做到这一点表达。一点点额外的信息应该可以帮助我学习更多。
Yes, I have investigated and read about the Date object, but I just can't seem to cook up the right expression. A bit of extra info should help me learn it more.
推荐答案
编辑:删除+1到一个月。原来是从副本&
removed +1 to month. Left it in originally from copy & paste of original code.
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth(); //January is 0!
var yyyy = today.getFullYear();
var events = cal.getEvents(new Date(yyyy, mm, dd, 0, 0, 0), new Date(yyyy, mm, dd, 23, 0, 0));
这篇关于如何在JavaScript中引用今天的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!