function dailyEventMessage() { var googleCalendarId = "xxxxxx@group.calendar.google.com"; var calendar = CalendarApp.getCalendarById(googleCalendarId); var today = new Date(); var tomorrow = new Date(today.getFullYear(), today.getMonth(),today.getDate()+1); var tomorrowEventList = calendar.getEventsForDay(tomorrow); const date = new Date(tomorrow) const tomorrowdateresult = date.toLocaleDateString('th-TH', { year: 'numeric', month: 'long', day: 'numeric', weekday: 'long', }) var message = ""; for (var i = 0; i < tomorrowEventList.length; i++) { var eventTitle = "Task: " + tomorrowdateresult + "\n" + "**"+ tomorrowEventList[i].getTitle()+"**"; var eventTime = "Timebegin: " + tomorrowEventList[i].getStartTime().toTimeString().slice(0,5)+"-"+ tomorrowEventList[i].getEndTime().toTimeString().slice(0,5); var eventDescription = "Details: " + tomorrowEventList[i].getDescription(); message += "\n" + eventTitle + "\n" + eventTime + "\n" + eventDescription + "\n"; } if (message === "") { return; } Logger.log(message); sendMessage(message); }我发现的问题是变量 eventtime,它显示为 12 小时格式。前任。03.00-04.00 但实际上我在谷歌日历中设置的时间为 15.00-16.00谁能建议如何将其更改为 24 小时格式。Thks
1 回答
人到中年有点甜
TA贡献1895条经验 获得超7个赞
如果您使用日期格式化程序,您的代码会更干净。通过使用toTimeString(),slice(0,5)你的代码有时会崩溃。
Intl.DateTimeFormat您可以将hour12配置设置为false. 它将为您提供 24 小时格式。
const startTime = new Date(2010, 11, 12, 13, 14);
const endTime = new Date(2010, 11, 12, 16, 17);
const formatter = new Intl.DateTimeFormat('en', {
hour: 'numeric',
minute: 'numeric',
hour12: false,
});
console.log(`${formatter.format(startTime)} - ${formatter.format(endTime)}`);
添加回答
举报
0/150
提交
取消