SQL Hive中的時間日期函式

2021-10-19 07:51:56 字數 3997 閱讀 9516

--時間戳轉日期

select from_unixtime(

1516723824

,'yyyy-mm-dd hh:mm:ss'

)--日期轉時間戳

select to_unix_timestamp(

'2021-01-05 12:32:41'

,'yyyy-mm-dd hh:mm:ss'

)select to_unix_timestamp(

'2021-01-05 12:32:41'

)select unix_timestamp(

'2021-01-05 12:32:41'

,'yyyy-mm-dd hh:mm:ss'

)select unix_timestamp(

'2021-01-05 12:32:41'

)

-- 獲取當期日期

select

current_date()

--輸出到日

--獲取當前日期和時間

select

current_timestamp()

/*輸出精確到毫秒*/

--獲取當前時間戳

select unix_timestamp(

)--輸出整數

--獲取當前時間(自定義輸出)

select from_unixtime(unix_timestamp(),

'yyyymmdd hh:mm:ss'

)

select unix_timestamp(

'20201223'

,'yyyymmdd'

)select unix_timestamp(

'2021-01-05 12:32:41'

,'yyyy-mm-dd hh:mm:ss'

)select unix_timestamp(

'2021-01-29t13:12:23z'

,"yyyy-mm-dd't'hh:mm:ss'z'"

)select unix_timestamp(

'25/jan/2021:12:32:23 +0800'

,'dd/mmm/yyyy:hh:mm:ss z'

)--mmm為3位字母月份

--兩個日期相差天數

select datediff(

'2021-02-01'

,'2020-11-15'

)-- 加上10天

select date_add(

'2021-02-01',10

)-- 返回 '2021-02-11'

-- sub版的加上10天

select date_sub(

'2021-02-01',-

10)-- 返回 '2021-02-11'

-- 加上5個月

select add_months(

'2020-11-05',5

)--返回 2021-04-05

-- 日期時間轉日期

select to_date(

'2021-02-01 12:30:15'

)-- 日期格式轉換

select date_format(

'2021-01-01 12:30:15'

,'yyyy|mm|dd'

)

--獲取年份

select from_unixtime(unix_timestamp(

'2020-05-29'

,'yyyy-mm-dd'),

'y年'

)----返回有字尾"年"

select

year

('2020-12-08 15:03:01'

)-- 獲取月份

select

month

('2020-12-08 15:03:01'

)--獲取日

select from_unixtime(unix_timestamp(

'2020-05-29'

,'yyyy-mm-dd'),

'd')

-- 本月的第幾天(29)

select

day(

'2020-12-08 15:03:01'

)-- 返回(8)

--獲取時

select

hour

('2020-12-08 15:03:01'

)-- 返回(15)

--獲取分

select

minute

('2020-12-08 15:03:01'

)-- 返回(3)

--獲取秒

select

second

('2020-12-08 15:03:01'

)--返回(1)

-- year,month,day,hour,minute,second都是直接從時間字串中擷取的相應部分

--星期幾

select from_unixtime(unix_timestamp(

'2020-05-29'

,'yyyy-mm-dd'),

'eeee'

)--返回結果 friday

--本週第幾天

select from_unixtime(unix_timestamp(

'2020-05-29'

,'yyyy-mm-dd'),

'u')

-- 星期一為第一天

select dayofweek(

'2021-02-09'

)--返回(3) 即星期日為第一天

--本年第幾天

select from_unixtime(unix_timestamp(

'2020-05-29'

,'yyyy-mm-dd'),

'd')

--本年第幾周

select from_unixtime(unix_timestamp(

'2020-05-29'

,'yyyy-mm-dd'),

'w')

--返回第幾周(22)

select weekofyear(

'2020-05-29'

)--本年第幾個季度

select quarter(

'2021-02-09'

)

-- 將utc時間轉為北京時間

select from_utc_timestamp(

'2021-01-01 00:00:00'

,'prc'

)--輸出 '2021-01-01 08:00:00'

-- 將北京時間轉為utc時間

select to_utc_timestamp(

'2021-01-01 00:00:00'

,'prc'

)--輸出 2020-12-31 16:00:00

-- 下乙個星期五

select next_day(

'2020-11-05'

,'fri'

)-- 返回2020-11-06

--本年第一天

select trunc(

'2021-02-09'

,'yy'

)--返回2021-01-01

--本年最後一天

select last_day(add_months(trunc(

'2021-02-09'

,'yy'),

11))--本月第一天

select trunc(

'2021-02-09'

,'mm'

)--返回2021-02-01

--本月最後一天

select last_day(

'2021-02-09'

)

reference:

時間日期函式

獲取 當前日期 curdate 2021 03 02 select curdate 獲取 當前時間 select curtime 獲取日期和時間 2021 03 02 14 47 31 select now 獲取日期所在的週數 第幾周 select week 1998 02 20 獲取日期中的年份 ...

SQLite中的時間日期函式

datetime 產生日期和時間 date 產生日期 time 產生時間 strftime 對以上三個函式產生的日期和時間進行格式化 datetime 的用法是 datetime 日期 時間,修正符,修正符.date 和time 的語法與datetime 相同。yyyy mm dd yyyy mm ...

c 時間日期函式

datetime dt datetime.now label1.text dt.tostring 2007 11 5 13 21 25 label2.text dt.tofiletime tostring 127756416859912816 label3.text dt.tofiletimeutc...