MySQL日期、字符串、时间戳互转
平时比较常用的时间、字符串、时间戳之间的互相转换,虽然常用但是几乎每次使用时候都喜欢去搜索一下用法;本文将作为一个笔记,整理一下三者之间的 转换(即:date转字符串、date转时间戳、字符串转date、字符串转时间戳、时间戳转date,时间戳转字符串)用法,方便日后查看;
涉及的函数
date_format(date, format) 函数,MySQL日期格式化函数date_format()
unix_timestamp() 函数
str_to_date(str, format) 函数
from_unixtime(unix_timestamp, format) 函数,MySQL时间戳格式化函数from_unixtime
时间转字符串
select date_format(now(), '%Y-%m-%d'); #结果:2016-01-05
时间转时间戳
select unix_timestamp(now()); #结果:1452001082
字符串转时间
select str_to_date('2016-01-02', '%Y-%m-%d %H');
#结果:2016-01-02 00:00:00
字符串转时间戳
select unix_timestamp('2016-01-02');
#结果:1451664000
时间戳转时间
select from_unixtime(1451997924); #结果:2016-01-05 20:45:24
时间戳转字符串
select from_unixtime(1451997924,'%Y-%d'); #结果:2016-01-05 20:45:24
附表
MySQL日期格式化(format)取值范围。
| 值 | 含义 | |
|---|---|---|
| 秒 | %S、%s | 两位数字形式的秒( 00,01, ..., 59) |
| 分 | %I、%i | 两位数字形式的分( 00,01, ..., 59) |
| 小时 | %H | 24小时制,两位数形式小时(00,01, ...,23) |
| %h | 12小时制,两位数形式小时(00,01, ...,12) | |
| %k | 24小时制,数形式小时(0,1, ...,23) | |
| %l | 12小时制,数形式小时(0,1, ...,12) | |
| %T | 24小时制,时间形式(HH:mm:ss) | |
| %r | 12小时制,时间形式(hh:mm:ss AM 或 PM) | |
| %p | AM上午或PM下午 | |
| 周 | %W | 一周中每一天的名称(Sunday,Monday, ...,Saturday) |
| %a | 一周中每一天名称的缩写(Sun,Mon, ...,Sat) | |
| %w | 以数字形式标识周(0=Sunday,1=Monday, ...,6=Saturday) | |
| %U | 数字表示周数,星期天为周中第一天 | |
| %u | 数字表示周数,星期一为周中第一天 | |
| 天 | %d | 两位数字表示月中天数(01,02, ...,31) |
| %e | 数字表示月中天数(1,2, ...,31) | |
| %D | 英文后缀表示月中天数(1st,2nd,3rd ...) | |
| %j | 以三位数字表示年中天数(001,002, ...,366) | |
| 月 | %M | 英文月名(January,February, ...,December) |
| %b | 英文缩写月名(Jan,Feb, ...,Dec) | |
| %m | 两位数字表示月份(01,02, ...,12) | |
| %c | 数字表示月份(1,2, ...,12) | |
| 年 | %Y | 四位数字表示的年份(2015,2016...) |
| %y | 两位数字表示的年份(15,16...) | |
| 文字输出 | %文字 | 直接输出文字内容 |
转载自:https://www.cnblogs.com/jhy-ocean/p/5560857.html
MySQL日期、字符串、时间戳互转的更多相关文章
- MySQL日期 字符串 时间戳互转
平时比较常用的时间.字符串.时间戳之间的互相转换,虽然常用但是几乎每次使用时候都喜欢去搜索一下用法:本文将作为一个笔记,整理一下三者之间的 转换(即:date转字符串.date转时间戳.字符串转dat ...
- 关于MYSQL日期 字符串 时间戳互转
时间转字符串: select date_format(now(), '%Y-%m-%d'); #结果:2016-01-05 时间转时间戳: select unix_timestamp(now()); ...
- mysql 日期 字符串 时间戳转换
#时间转字符串 select date_format(now(), '%Y-%m-%d'); -02-27 #时间转时间戳 select unix_timestamp(now()); #字符串转时间 ...
- MySQL日期与时间戳互转函数
-- 时间戳转日期 ); #日期转时间戳 Select UNIX_TIMESTAMP('2018-07-16 12:23:00');
- MySql 日期字符串类型互转
1.data_format 日期转字符串 select date_format(Now(), '%Y-%m-%d %H:%i'); 2.str_to_date 字符串转日期 select str_to ...
- [转]MySQL日期与时间戳常用函数
本文原文地址:https://www.cnblogs.com/jhy-ocean/p/5560857.html 平时比较常用的时间.字符串.时间戳之间的互相转换,虽然常用但是几乎每次使用时候都喜欢去搜 ...
- mysql 日期 字符串
Mysql 中字符串转时间跟Oracle略不同,函数为 str_to_date 应注意的是里面的大小写 如下: MySQL内置函数,在mysql里面利用str_to_date()把字符串转换为日期. ...
- Mysql 日期-字符串转换。
mysql的字符串和日期类型的转换. 1.now()和curdate()的区别: now():datetime类型. mysql> select now(); +---------------- ...
- MySQL 日期和时间戳互相转换
① 时间戳转换成日期 FROM_UNIXTIME 例如: 数据表中 invest_time 存储的是时间戳,如 1429063399 使用 FROM_UNIXTIME 可以把时间戳转换为日期: sel ...
- JavaScript 日期与时间戳互转
1.时间戳转日期格式: function timestampToTime(timestamp) { var date = new Date(timestamp * 1000);//时间戳为10位需*1 ...
随机推荐
- windows8安装msi软件提示2503错误的解决办法
windows8以后的版本安装msi软件(比如nodejs.msi.Git.msi.python.msi.T ortoiseSVN.msi)的时候老师出现2503.2502的错误,究其原因还是系统权限 ...
- 洛谷p1064 金明的预算方法
有附带条件的01背包 要那附件必须拿主件 因为一个主件最多有两个附件,所以每次遇到主件可能有四种选择 1.只拿主件 2.拿主件和一号附件 3.拿主件和二号附件 4.都拿 #include <cs ...
- web.xml的加载过程是context-param >> listener >> fileter >> servlet
web.xml的加载过程是context-param >> listener >> fileter >> servlet
- Vue 问题记录
Vue 问题记录 汇总日常开发中遇到的关于vue的问题 VeeValidator 语言设置 校验消息默认是英文的,定义中文或其他语言的错误提示消息 import VeeValidate from 'v ...
- spring+jdbc+template+transaction实现
使用spring和jdbc模板事务实现 1.创建实体类: Role package com.wbg.sjt.entity; public class Role { private int id; pr ...
- HTMLFormElement获取表单里面所有的值然后以json形式返回
function HTMLFormElement(){ this.init(); return this.json; } HTMLFormElement.prototype.init = functi ...
- The App Life Cycle & The Main Function
The App Life Cycle Apps are a sophisticated interplay between your custom code and the system framew ...
- render 函数渲染表格的当前数据列使用
columns7: [ { title: '编号', align: 'center', width: 90, key: 'No', render: (h, params) => { return ...
- ORA-00911: 无效字符 问题和解决
1.原本java插入数据库表数据写法是这样的 String sql = "INSERT INTO AAA (id1,id2,id3,id4) VALUES ('1','2','3','4') ...
- IDEA中使用单元测试@Test等,提示没有 Junit.jar包
1.File-->Project Structure-->Modules-->右侧Dependencies-->+号-->JARs or directories... 2 ...