MySQL date and time addition and subtraction sample code

MySQL date and time addition and subtraction sample code

I was reviewing MySQL recently and happened to see MySQL date and time. I will leave a note for myself and share it with you.

  • now (); Current specific date and time
  • curdate(); Current date
  • curtime(); Current time

1.MySQL adds or subtracts a time interval

Set the current date variable

set @dt = now(); //Set the current date select @dt; //Query variable value 

Adding and subtracting a time interval functions date_add() and date_sub()

date_add('a certain date and time', interval 1 time type name);

Example:

select date_add(@dt, interval 1 year); //add 1 yearselect date_add(@dt, interval 1 month); //add 1 month

quarter: quarter, week: week, day: day, hour: hour, minuter: minute, second: second, microsecond: millisecond

Note: You can also add or subtract a time directly without using variables, such as: select date_add('1998-01-01', interval 1 day);

2. Subtract dates

datediff(date1,date2): Subtract two dates, date1 minus date2 to get the number of days after the subtraction

timediff(time1, time2): Subtract time1 from time2 and return the difference.


select timediff('2019-06-03 12:30:00', '2019-06-03 12:29:30');

Equivalent to

select timediff('12:30:00', '12:29:30');

This is the end of this article about sample code for MySQL date and time addition and subtraction. For more information about MySQL date and time addition and subtraction, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of MySQL date addition and subtraction functions

<<:  HTML form value transfer example through get method

>>:  Detailed process of installing the docker plugin in IntelliJ IDEA (2018 version)

Recommend

Detailed usage of Vue timer

This article example shares the specific code of ...

Three Discussions on Iframe Adaptive Height Code

When building a B/S system interface, you often en...

Navicat remote connection to MySQL implementation steps analysis

Preface I believe that everyone has been developi...

Solution to nacos not being able to connect to mysql

reason The mysql version that nacos's pom dep...

Summary of MySQL database like statement wildcard fuzzy query

MySQL error: Parameter index out of range (1 >...

Summary of basic knowledge points of MySql database

Table of contents Basic database operations 2) Vi...

Detailed Example of CSS3 box-shadow Property

CSS3 -- Adding shadows (using box shadows) CSS3 -...

JavaScript implements fireworks effects with sound effects

It took me half an hour to write the code, and th...

Solution for forgetting the root password of MySQL5.7 under Windows 8.1

【background】 I encountered a very embarrassing th...

Detailed explanation of the use of DockerHub image repository

Previously, the images we used were all pulled fr...

Understanding and example code of Vue default slot

Table of contents What is a slot Understanding of...

mysql backup script and keep it for 7 days

Script requirements: Back up the MySQL database e...

Use of Linux relative and absolute paths

01. Overview Absolute paths and relative paths ar...

Solution to the failure of 6ull to load the Linux driver module

Table of contents 0x01 Failed to load the driver ...