date.truncate() 函数
date.truncate()
返回截断为指定持续时间单位的时间。
函数类型签名
(t: A, unit: duration, ?location: {zone: string, offset: duration}) => time where A: Timeable
有关更多信息,请参阅 函数类型签名。
参数
t
(必需) 要操作的时间。
使用绝对时间、相对持续时间或整数。持续时间相对于 now()
。
unit
(必需) 要截断到的时间单位。
仅使用 1 和时间单位来指定单位。例如:1s
、1m
、1h
。
location
用于确定时区的 Location。默认为 location
选项。
示例
截断时间值
import "date"
import "timezone"
option location = timezone.location(name: "Europe/Madrid")
date.truncate(t: 2019-06-03T13:59:01Z, unit: 1s)
// Returns 2019-06-03T13:59:01.000000000Z
date.truncate(t: 2019-06-03T13:59:01Z, unit: 1m)
// Returns 2019-06-03T13:59:00.000000000Z
date.truncate(t: 2019-06-03T13:59:01Z, unit: 1h)
// Returns 2019-06-03T13:00:00.000000000Z
date.truncate(t: 2019-06-03T13:59:01Z, unit: 1d)
// Returns 2019-06-02T22:00:00.000000000Z
date.truncate(t: 2019-06-03T13:59:01Z, unit: 1mo)
// Returns 2019-05-31T22:00:00.000000000Z
date.truncate(t: 2019-06-03T13:59:01Z, unit: 1y)// Returns 2018-12-31T23:00:00.000000000Z
使用相对持续时间截断时间值
import "date"
option now = () => 2020-01-01T00:00:30.5Z
date.truncate(t: -30s, unit: 1s)
// Returns 2019-12-31T23:59:30.000000000Z
date.truncate(t: -1m, unit: 1m)
// Returns 2019-12-31T23:59:00.000000000Z
date.truncate(t: -1h, unit: 1h)// Returns 2019-12-31T23:00:00.000000000Z
查询今年数据
import "date"
from(bucket: "example-bucket")
|> range(start: date.truncate(t: now(), unit: 1y))
查询本月日历数据
import "date"
from(bucket: "example-bucket")
|> range(start: date.truncate(t: now(), unit: 1mo))
此页是否对您有帮助?
感谢您的反馈!