简体   繁体   English

如何在日期 object 中使用 24 小时格式?

[英]how to use 24 hour format in date object?

In my example it writes inside calendar hours in 12am-pm format, what should I change?在我的示例中,它以 12am-pm 格式在日历时间内写入,我应该更改什么? And is it possible to make your own format?是否可以制作自己的格式? For example if I want change 2021-01-27 8:00 to 2021.01.27 8:00 .例如,如果我想将2021-01-27 8:00更改为2021.01.27 8:00

function myFunction1() {
  var calendar = CalendarApp.getCalendarById('callendar id');
      calendar.createEvent("my title",new Date ('2021-01-27 8:00'),new Date ('2021-01-27 13:00'));
  }

Most importantly, dates have no idea what format they are in. It depends on the environment and output settings.最重要的是,日期不知道它们的格式。这取决于环境和 output 设置。

Configure your environment correctly正确配置您的环境

Specify the correct timezone in the manifest在清单中指定正确的时区

Your project manifest must be set up for some time.您的项目清单必须设置一段时间。 It is important.这很重要。

在此处输入图像描述

Customize the time display in Calendar as you need根据需要自定义日历中的时间显示

You have to remember that dates are not what we see, they are more complex objects of information.您必须记住,日期不是我们所看到的,它们是更复杂的信息对象。 What we see are strings from the dates.我们看到的是日期中的字符串。

在此处输入图像描述

Let's code让我们编码

The next code is correct下一个代码是正确的

calendar
  .createEvent(
    "my title",
    new Date('2021-01-27 8:00'),
    new Date('2021-01-27 13:00')
  );

because there is a correct signature for call new Date()因为调用new Date()有正确的签名

The next code is correct too下一个代码也是正确的

calendar
  .createEvent(
    "my title2",
    new Date('2021-02-27 8:00'),
    new Date('2021-02-27 8:00')
  );

It creates an event with no duration.它创建一个没有持续时间的事件。 This is fine.这可以。 This is logically expected.这是合乎逻辑的预期。

To specify the interval you need specify noon time or 24 hour format:要指定间隔,您需要指定中午时间或 24 小时格式:

calendar
  .createEvent(
    "my title2",
    new Date('2021-02-27 8:00'),
    new Date('2021-02-27 20:00')
  );

or或者

calendar
  .createEvent(
    "my title2",
    new Date('2021-02-27 8:00 AM'),
    new Date('2021-02-27 8:00 PM')
  );

Usually the duration is simply added to the start time.通常,持续时间只是简单地添加到开始时间。 Like that像那样

function createEvent() {
  var calendar = CalendarApp.getDefaultCalendar();
  const start = new Date('2021-02-27 8:00');
  const end = new Date(start);
  end.setHours(end.getHours() + 12);
  calendar
    .createEvent("my title2", start, end);
}

Yes, you can use your own formats for creating date:是的,您可以使用自己的格式来创建日期:

function createEvent() {
  var calendar = CalendarApp.getDefaultCalendar();
  const start = newDate('27.02.2021 8:00');
  const end = new Date(start);
  end.setHours(end.getHours() + 12);
  calendar
    .createEvent("my title2", start, end);
}

function newDate(string){
  const match = string.match(/(\d{1,2}).(\d{1,2}).(\d{4})\s+(\d{1,2}:\d{1,2})/);
  if(!match) throw new Error('Invalid string');
  return new Date(`${match[3]}-${match[2]}-${match[1]} ${match[4]}`);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM