更新时间:2024-01-17 GMT+08:00
分享

日期函数概览

DLI所支持的日期函数如表1所示。

表1 日期/时间函数

函数

命令格式

返回值

功能简介

add_months

add_months(string start_date, int num_months)

STRING

返回start_date在num_months个月之后的date。

current_date

current_date()

DATE

返回当前日期,如2016-07-04。

current_timestamp

current_timestamp()

TIMESTAMP

返回当前时间,如2016-07-04 11:18:11.685。

date_add

date_add(string startdate, int days)

STRING或DATE

给定时间,在此基础上加上指定的时间段。

dateadd

dateadd(string date, bigint delta, string datepart)

STRING或DATE

dateadd函数用于按照指定的单位datepart和幅度delta修改date的值。

date:必填。日期值,string类型。

使用的时间格式为yyyy-mm-dd hh:mi:ss,例如2021-08-28 00:00:00。

delta:必填。修改幅度,BIGINT类型。

datepart:必填。指定修改的单位,STRING类型常量。

date_sub

date_sub(string startdate, int days)

STRING

给定时间,在此基础上减去指定的时间段。

date_format

date_format(string date, string format)

STRING

用于将date按照format指定的格式转换为字符串。

datediff

datediff(string date1, string date2)

BIGINT

datediff函数用于计算两个时间date1、date2的日期差值。

datediff1

datediff1(string date1, string date2, string datepart)

BIGINT

datediff1函数用于计算两个时间date1、date2的差值,将差值以指定的时间单位datepart表示。

datepart

datepart(string date,string datepart)

BIGINT

取日期中符合指定时间单位的字段值。

datetrunc

datetrunc (string date, string datepart)

STRING

datetrunc函数用于计算将日期date按照datepart指定的时间单位进行截取后的日期值。

date:必填。格式为yyyy-mm-dd或yyyy-mm-dd hh:mi:ss。

datepart:必填。STRING类型常量。支持扩展的日期格式。

day/dayofmonth

day(string date)、 dayofmonth(string date)

INT

返回指定时间的日期。

from_unixtime

from_unixtime(bigint unixtime)

STRING

将时间戳转换为时间格式,格式为“yyyy-MM-dd HH:mm:ss”或“yyyyMMddHHmmss.uuuuuu”。

例如:select FROM_UNIXTIME(1608135036,'yyyy-MM-dd HH:mm:ss')

from_utc_timestamp

from_utc_timestamp(string timestamp, string timezone)

TIMESTAMP

将UTC的时间戳转化为timezone所对应的时间戳。

getdate

getdate()

STRING

获取当前系统时间。

hour

hour(string date)

INT

返回指定时间的小时,范围为0到23。

isdate

isdate(string date , string format)

BOOLEAN

date:必填。STRING类型。会隐式转换为STRING类型后参与运算。

format:必填。STRING类型常量,不支持日期扩展格式。如果format中出现多余的格式串,则只取第一个格式串对应的日期数值,其余的会被视为分隔符。例如isdate("1234-yyyy", "yyyy-yyyy"),会返回True。

last_day

last_day(string date)

DATE

返回date所在月份的最后一天,格式为yyyy-MM-dd,如2015-08-31。

lastday

lastday(string date)

STRING

lastday函数用于返回date所在月的最后一天,返回值STRING类型,格式为yyyy-mm-dd hh:mi:ss。

minute

minute(string date)

INT

返回指定时间的分钟,范围为0到59。

month

month(string date)

INT

返回指定时间的月份,范围为1至12月。

months_between

months_between(string date1, string date2)

DOUBLE

返回date1与date2之间的月份差。

next_day

next_day(string start_date, string day_of_week)

DATE

返回start_date之后最接近day_of_week的日期,格式为yyyy-MM-dd,day_of_week表示一周内的星期(如Monday、FRIDAY)。

quarter

quarter(string date)

INT

返回该date/timestamp/string所在的季度,如quarter('2015-04-01')=2。

second

second(string date)

INT

返回指定时间的秒,范围为0到59。

to_char

to_char(string date, string format)

STRING

将日期按照指定格式转换为字符串。

to_date

to_date(string timestamp)

DATE

返回时间中的年月日,例如:to_date("1970-01-01 00:00:00") = "1970-01-01"

to_date1

to_date1(string date, string format)

STRING

返回STRING类型,格式为yyyy-mm-dd hh:mi:ss。date或format值为NULL时,返回NULL。

将指定格式的字符串转换为日期值。

to_utc_timestamp

to_utc_timestamp(string timestamp, string timezone)

TIMESTAMP

将timezone所对应的时间戳转换为UTC的时间戳。

trunc

trunc(string date, string format)

DTAE

将date按照特定的格式进行清零操作,支持格式为MONTH/MON/MM, YEAR/YYYY/YY,如trunc('2015-03-17', 'MM') = 2015-03-01。

unix_timestamp

unix_timestamp(string timestamp, string pattern)

BIGINT

如果不带参数的调用,返回一个Unix时间戳(从“1970-01-01 00:00:00”到现在的秒数)为无符号整数。

weekday

weekday (string date)

INT

返回日期值是当前周的第几天。

weekofyear

weekofyear(string date)

INT

返回指定日期是一年中的第几周,范围为0到53。

year

year(string date)

INT

返回指定日期中的年份。

分享:

    相关文档

    相关产品