datawind可视化查询-日期函数

飞书文档学习链接:https://www.volcengine.com/docs/4726/47275

1 常用函数快速入门

日期函数用于作时间格式转换、时间处理、获取指定日期等。

1.1 将文本转换为时间或日期

格式:

  • toDate(xxx)和 to_date(xxx),将 yyyymmdd 或 yyyy-mm-dd 样式的字符串字段转换成日期格式
  • toDateTime(xxx),将 yyyy-mm-dd hh:mm:ss 样式的字符串转换成时间格式
  • toDateOrNull(xxx) 和 toDateTimeOrNull(xxx),功能与上述 1 和 2 相同,适用于字段中存在脏数据的情况,会将脏数据转换成空值

示例:

字段A

toDate(字段A)

toDateTime(字段A)

2020/12/21

2020/12/21

2020/12/21 0:00

20201221

2020/12/21

2020/12/21 0:00

2020/12/21 18:00

2020/12/21

2020/12/21 18:00

字段A为字符串类型,toDate(字段A)和 toDateTime(字段A)为时间格式

1.2 时间与时间戳相互转换

格式:将时间或文本类型的时间转换成时间戳 toUnixTimestamp(xxx),即将 yyyy-mm-dd hh:mm:ss 转换成时间戳

示例:

字段A

toUnixTimestamp(字段A)

扩展:toDateTime(toUnixTimestamp(字段A)+28800)

2020/12/21 18:00

1608544800

2020/12/22 2:00

扩展:对转换后的时间戳,加上 8 小时对应的时间戳差值 28800,完成从 UTC+0 到 UTC+8 的转换,然后再通过 toDateTime 函数转换成可读性更强的时间格式。

1.3 返回当前日期

格式:now(),生成一个随系统实时时间变化的动态参数

示例:现在时间是 2020-12-21 18:00:00,写下 now()作为一个字段,则这个字段当前值为 2020-12-21 18:00:00,一小时后为 2020-12-21 19:00:00

1.4 返回本周周一的日期

格式:toMonday(xx),对日期字段向前取到本周一对应的日期

示例:

字段A

toMonday(字段A)

2020/12/22

2020/12/21

2 功能详解

函数名称

函数格式

用途

示例

toDateTime

toDateTime(x)

将字符型时间日期转化为时间日期型

toDateTime('2018-12-24 10:10:00')

toStartOfFiveMinute

toStartOfFiveMinute(x)

截取时间日期到最近的 5 的倍数分钟(之后归零),返回日期

toStartOfFiveMinute(toDateTime('2018-12-11 11:12:13')) 返回 2018-12-11 11:10:00

now

now()

生成当前时间日期

now( ) 返回 2018-12-13 10:10:12

toStartOfFifteenMinutes

toStartOfFifteenMinutes(x)

截取时间日期到最近的 15 的倍数分钟(之后归零),返回日期

toStartOfFifteenMinute(toDateTime('2018-12-11 11:12:13')) 返回 2018-12-11 11:00:00

toStartOfInterval

toStartOfInterval(time_or_data, INTERVAL x unit [, time_zone])

这是名为 toStartOf*的所有函数的通用函数。例如,toStartOfInterval(t,INTERVAL 1 year)返回与 toStartOfYear(t)相同的结果,toStartOfInterval(t,INTERVAL 1 month)返回与 toStartOfMonth(t)相同的结果, toStartOfInterval(t,INTERVAL 1 day)返回与 toStartOfDay(t)相同的结果, toStartOfInterval(t,INTERVAL 15 minute)返回与 toStartOfFifteenMinutes(t)相同的结果。

toStartOfInterval(toDateTime('2019-10-10 09:09:20'),INTERVAL 1 year) 返回 2019-01-01

toIntervalDay

toIntervalDay(x)

日期转化为时间戳,天级别

toIntervalDay(toDateTime('2019-08-12 11:12:13')) 返回 1565579533

toIntervalMinute

toIntervalMinute(x)

日期转化为时间戳,分钟级别

toIntervalMinute(toDateTime('2019-08-14 11:12:13')) 返回 1565579533

toIntervalHour

toIntervalHour(x)

日期转化为时间戳,小时级别

toIntervalHour(toDateTime('2019-08-14 11:12:13')) 返回 1565579533

toMonday

toMonday(x)

将 Date 或 DateTime 向前取整到本周的星期一。 返回 Date 类型。

toMonday(toDateTime('2019-08-14 11:12:13')) 返回 2019-08-12

toStartOfHour

toStartOfHour(x)

将DateTime向前取整到当前小时的开始。

toStartOfHour(toDateTime('2018-12-11 11:12:13')) 返回 11

today

today( )

生成今天的日期

today( ) 返回 2018-12-13

toHour

toHour(x)

取时间日期的小时

toHour(toDateTime('2018-12-11 11:12:13')) 返回 11

toStartOfTenMinutes

toStartOfTenMinutes(x)

将 DateTime 以十分钟为单位向前取整到最接近的时间点

toStartOfTenMinute(toDateTime('2018-12-11 11:12:13')) 返回 2018-12-11 11:00:00

toStartOfDay

toStartOfMinute(string date time)

将 DateTime 向前取整到当日的开始。

toYear

toYear(string date)

返回日期中的年

toRelativeWeekNum

toRelativeWeekNum(date)

将 Date 或 DateTime 转换为星期数,从过去的某个固定时间点开始。

toMonth

toMonth(string date)

返回日期中的月

toStartOfMinute

toStartOfMinute( )

截取时间日期到分钟(之后归零),返回日期

toStartOfMinute(toDateTime('2018-12-11 11:12:13')) 返回 2018-12-11 11:12:00

toUnixTimestamp

toUnixTimestamp( )

将 DateTime 转换为 unix 时间戳

addDays

addDays(date_time,int)

函数将一段时间间隔添加到 Date/DateTime,然后返回 Date/DateTim

toRelativeYearNum

toRelativeYearNum(date_time)

将 Date 或 DateTime 转换为年份的编号,从过去的某个固定时间点开始

year

year(Date/DateTime/String date)

返回日期中的年

year(toDateTime('2018-12-11 11:12:13'))返回 2018

month

month(Date/DateTime/String date)

返回日期中的月

month(toDateTime('2018-12-11 11:12:13'))返回 12

quarter

quarter(Date/DateTime/String date)

返回日期中的季度数

quarter(toDateTime('2018-12-11 11:12:13'))返回 12

day

day(Date/DateTime/String)

返回 dayOfMonth

day('2019-09-12') 返回 12

last_day

last_day(Date/DateTime/String date)

返回日期所在的月份的最后一天

last_day(toDateTime('2018-12-11 11:12:13'))返回 2018-12-31

next_day

next_day(Date/DateTime/String date, string day_of_week)

返回日期中的下一个星期day_of_week

next_day(toDate('2015-01-14'), 'TU') 返回 2015-01-20

dateDiff

dateDiff(Date/DateTime date1, Date/DateTime date2 [, String timezone]); dateDiff(String unit, Date/DateTime date1, Date/DateTime date2 [, String timezone])

返回两个日期的差值

dateDiff(now(), yesterday())返回 -1

date_sub

date_sub (string/Date/DateTime startdate, int days)

返回start_time 前 days 天的日期/时间

date_sub('2019-09-08', 1) 返回'2019-09-07'

date_add

date_add (string/Date/DateTime startdate, int days)

返回start_time 后 days 天的日期/时间

date_add('2019-09-08', 1) 返回'2019-09-09'

from_unixtime

from_unixtime(Date/DateTime/UInt32 date, String parttern[, String timezone])

将日期或时间戳转换为 pattern 形式的字符串

from_unixtime(toDateTime('2019-09-09 09:00:00'), 'time: %F %T', 'Asia/Shanghai') 返回'time: 2019-09-09 09:00:00'

to_date,toDate

to_date(String/UInt16 date)

将字符串/自 1970-01-01 起的第 date 天转换为日期类型

toDate('20190909') 返回'2019-09-09'

date_format

date_format(Date/DateTime/String, String pattern)

将日期按指定格式输出

date_format('2019-09-09', 'yyyy') 返回 '2019'

unix_timestamp

unix_timestamp(Date/DateTime/String)

将日期转换为 timestamp

unix_timestamp(toDateTime('2019-09-09 09:00:00'), 'time: %F %T', 'Asia/Shanghai') 返回'time: 2019-09-09 09:00:00'

toWeek

toWeek(date[,mode])

返回日期所在的周, 范围[0, 53]

toWeek(toDate('2019-12-06'), 3) 返回 49

toDayOfWeek

todayofweek(date)

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

法迪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值