Oracle常用時間函式

2021-10-10 19:41:19 字數 2859 閱讀 1796

設定時間格式

alter session set nls_date_format =

'dd-mon-yyyy hh:mi:ss'

獲取系統時間1
select sysdate from dual;
獲取系統時間2
select current_date from dual;
將日期轉為字串
select

to_char

(sysdate,

'yyyy-mm-dd hh24:mi:ss'

)from dual;

將字串轉為日期格式
select

to_date

('2017-09-22 00:00:00'

,'yyyy-mm-dd hh24:mi:ss'

) from dual;

轉為時間戳格式
select to_timestamp

(to_char

(sysdate,

'yyyy-mm-dd hh24:mi:ss'),

'yyyy-mm-dd hh24:mi:ssxff'

) from dual

時間加減運算
過去一小時/一分鐘/一秒

select to_char

(to_date

('2019/06/04 14:43:51'

,'yyyy/mm/dd hh24:mi:ss')-

1/24,

'yyyy/mm/dd hh24:mi:ss'

) from dual

過去一天

select to_char

(sysdate -1,

'yyyy/mm/dd hh24:mi:ss'

) from dual

過去乙個月

select add_months

(sysdate,-1

) from dual

獲取年月日時分秒
獲取時間的年

select

to_char

(sysdate,

'yyyy'

) as nowyear from dual;

獲取時間的月

select

to_char

(sysdate,

'mm'

) as nowmonth from dual;

獲取時間的日

select

to_char

(sysdate,

'dd'

) as nowday from dual;

獲取時間的時

select

to_char

(sysdate,

'hh24'

) as nowhour from dual;

獲取時間的分

select

to_char

(sysdate,

'mi'

) as nowminute from dual;

獲取時間的秒

select

to_char

(sysdate,

'ss'

) as nowsecond from dual;

獲取日期的星期幾

select

to_char

(sysdate,

'day'

) from dual;

兩個日期間的天數

select

floor

(sysdate -

to_date

('20020405'

,'yyyymmdd'

)) from dual;

時間為null的用法
select

to_date

(null) from dual;

找出今年的天數
select

add_months

(trunc

(sysdate,

'year'),

12)-trunc

(sysdate,

'year'

) from dual

獲取季度
select to_char

(sysdate,

'q') from dual;

不同時區的處理
select

to_char

(new_time

( sysdate,

'gmt'

,'est'),

'dd/mm/yyyy hh:mi:ss'

),sysdate from dual;

常用時間函式

比較常用的時間函式有time localtime asctime 和gmtime 函式time 的原型為 time t time time t time 函式time 返回系統的當前日曆時間,如果系統丟失時間設定,則函式返回 1。對函式time的呼叫,既可以使用空指標,也可以使用指向time t型別...

ORACLE常用時間查詢

取當前日期的本月末 select to char last day trunc to date 20200903 yyyymmdd month yyyymmdd 本月末 from dual select to char last day trunc to date 20200903 yyyymmdd...

php 常用時間函式

php時間 time 來取得伺服器當前時間的時間戳 expiration time 2 3600 得到當前時間延遲2小時候的時間戳 失效驗證 略 計算當前頁面執行時間的例子 do something sleep 3 do something running time time server requ...