Mysql日期常用操作

2021-10-01 10:45:09 字數 1716 閱讀 4872

timestamp時間戳格式

timestamp格式的使用期限是: 1970-1-1 到 2023年; timestamp格式的字段, 可以在行記錄更新時, 自動更新時間.

表設計如下:

執行update t_user set birthday='1980-2-12 17:09:14' where user_id=1, 可操作 birthday欄位

date_format(『日期類欄位』, 『格式化字段』)

常用佔位符:

%y 年

%m 月

%d day of month %j day of year

%t 時(24小時制) %r 時(12小時, am pm)

%i 分

%s 秒

舉例:

# 這裡檢視所有記錄(自行插入)

# 格式化日期輸出

select user_id, user_name, date_format(birthday,'%y年%m月%d日') as birthday

更多佔位符可見

從日期中取值:

計算時間差

# 以日為單位計算(向下取整)

select timestampdiff(day,birthday,now())

from t_user;

# 以年為單位計算(向下取整)

select timestampdiff(year,birthday,now())

from t_user;

直接用不等號比較

select * from t_user

where birthday between '1993-3-2' and '1995-3-3'

select * from t_user

where birthday >= '1993-3-2'

and birthday <= '1995-3-3'

兩個查詢得到一樣的結果

mysql 日期操作

今天 select from 表名 where to days 時間欄位名 to days now 昨天 select from 表名 where to days now to days 時間欄位名 1 本週 select from 表名 where yearweek date format 時間欄...

mysql日期操作

一 獲取當前日期時間 1.1 獲得當前日期 時間 date time 函式 now 1.2 獲取當前日期 時間 date time 函式 sysdate 注 二者類擬,不同在於now 在執行時就得到了,sysdate 執行時動態得到。2 獲取當前日期 curdate 與current date 等同...

java常用日期操作

根據開始日期 需要的工作日天數 計算工作截止日期,並返回截止日期 param startdate 開始日期 param workday 工作日天數 周一到周五 return date型別 createtime 2014 2 14 author sunqinbo public static date ...