跳转到主要内容

日期操作

本库包含 date 对象,提供一组日期格式化方法。

在本文中,我们将重点介绍一些特别重要且常用的方法。完整的方法列表可以在 date 对象页面 找到。

将 Date 对象转换为字符串

要将 Date 对象转换为字符串,请使用 date_to_str 方法:

该方法返回一个将 Date 对象转换为指定格式字符串的函数:

const formatFunc = scheduler.date.date_to_str("%d/%m/%Y");
const date = formatFunc(new Date(2027, 05, 29)); // -> "29/06/2027"

在周视图中显示多周

将字符串转换为 Date 对象

要将字符串转换为 Date 对象,请使用 str_to_date 方法:

该方法返回一个将指定格式的字符串转换为 Date 对象的函数:

你可以按以下方式生成日期转换函数:

const formatFunc = scheduler.date.str_to_date("%d/%m/%Y");
const date = formatFunc("29/06/2027"); // -> 29 June, 2027 00:00:00

转换为 UTC

要将本地时间转换为 UTC,请使用 convert_to_utc 方法:

//29 June, 2027 14:00 (local time) -> 29 June, 2027 12:00 (utc)
const time = scheduler.date.convert_to_utc(new Date(2027, 05, 29, 14, 00));

向日期添加(或减少)时间间隔

要向指定日期添加(或减少)时间间隔,请使用 add 方法:

//为指定日期添加1年:2027年6月29日 -> 2028年6月29日
const newDate = scheduler.date.add(new Date(2027, 05, 29), 1, 'year');

更改 Y 轴步长

注释

完整的日期格式化方法列表,请参见 此处

Need help?
Got a question about the documentation? Reach out to our technical support team for help and guidance. For custom component solutions, visit the Services page.