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 explanation of Vue3's responsive principle

Table of contents Review of Vue2 responsive princ...

How to use nginx to configure access to wgcloud

The nginx configuration is as follows: Such as ht...

How to change password in MySQL 5.7.18

How to change the password in MySQL 5.7.18: 1. Fi...

How to test network speed with JavaScript

Table of contents Preface Summary of the principl...

The implementation process of Linux process network traffic statistics

Preface Linux has corresponding open source tools...

Example of converting spark rdd to dataframe and writing it into mysql

Dataframe is a new API introduced in Spark 1.3.0,...

Implementation steps for building multi-page programs using Webpack

It is very common to use webpack to build single-...

MYSQL subquery and nested query optimization example analysis

Check the top 100 highest scores in game history ...

Detailed explanation of template tag usage (including summary of usage in Vue)

Table of contents 1. Template tag in HTML5 2. Pro...

Tips for writing concise React components

Table of contents Avoid using the spread operator...

Vue custom directive details

Table of contents 1. Background 2. Local custom i...

How to implement the webpage anti-copying function (with cracking method)

By right-clicking the source file, the following c...

jQuery canvas generates a poster with a QR code

This article shares the specific code for using j...

Detailed explanation of MySQL combined index and leftmost matching principle

Preface I have seen many articles about the leftm...