数据类型 | 占用空间 | 取值范围 | 零值 |
---|
DATE | 3 | ‘1000-01-01’ to ‘9999-12-31’ | ‘0000-00-00’ |
DATETIME | 8 | ‘1000-01-01 00:00:00’ to ‘9999-12-31 23:59:59’ | ‘0000-00-00 00:00:00’ |
TIMESTAMP | 4 | ‘1970-01-01 00:00:01’ UTC to ‘2038-01-19 03:14:07’ UTC | ‘0000-00-00 00:00:00’ UTC |
TIME | 3 | ‘-838:59:59’ to ‘838:59:59’ | ‘00:00:00’ |
YEAR | 1 | 1970 to 2155 (4 digit) or 2000 to 2999 (2 digit) | ‘00’ (2 digit) or ‘1970’ (4 digit) |
create database DateAndTimeStudy;
use DateAndTimeStudy;
create table DateAndTimeType(e_date date,e_datetime datetime ,e_timestamp timestamp ,e_time time ,e_year year );
insert into DateAndTimeType values('2024-01-09','2024-01-09 13:14:52','2024-01-09 10:23:16','13:14:52','2024');
select *from dateandtimetype;
insert into DateAndTimeType values('2024/01/09','2024_01-09 10:00:01','2024/01-09 10/23/16','13:14:13','2024');
select *from dateandtimetype;
select curdate();
select Now();
select time(now());
select time('2024-01-09 10:00:01');
select year(now());
select month(now());
select day(now());
select hour(now());
select minute(now());
select second(now());
select *from dateandtimetype;
insert into dateandtimetype values(curdate(),now(),now(),time(now()),year(now()));
select* from dateandtimeType;
select *from dateandtimetype where e_time='xx:xx:xx';
函数 | 描述 |
---|
curdate() | 获取当前的日期 |
Now() | 获取当前日期和时间 |
time(now()) | 获取当前时间 |
time(‘xx-xx-xx xx:xx:xx’) | 获取指定日期和时间的时间部分 |
year(now()) | 获取当前日期的年份 |
month(now()) | 获取当前日期的月份 |
day(now()) | 获取当前日期的天数部分 |
hour(now()) | 获取当前日期的小时数部分 |
minute(now()) | 获取当前日期的分钟数部分 |
second(now()) | 获取当前日期的秒数部分 |