如何转换为 24 小时格式(谷歌应用程序脚本)

 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


交互式爱情
浏览 78回答 1
1回答

人到中年有点甜

如果您使用日期格式化程序,您的代码会更干净。通过使用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', {&nbsp; &nbsp; hour: 'numeric',&nbsp; &nbsp; minute: 'numeric',&nbsp; &nbsp; hour12: false,});console.log(`${formatter.format(startTime)} - ${formatter.format(endTime)}`);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript