{"id":29075,"date":"2024-04-16T09:30:40","date_gmt":"2024-04-16T08:30:40","guid":{"rendered":"https:\/\/nelkodev.com\/blog\/domina-las-fechas-en-mysql-una-guia-completa-sobre-el-tipo-date\/"},"modified":"2024-06-03T17:44:16","modified_gmt":"2024-06-03T16:44:16","slug":"domina-las-fechas-en-mysql-una-guia-completa-sobre-el-tipo-date","status":"publish","type":"post","link":"https:\/\/nelkodev.com\/en\/blog\/master-dates-in-mysql-a-complete-guide-on-the-date-type\/","title":{"rendered":"Master Dates in MySQL: A Complete Guide to the DATE Type"},"content":{"rendered":"<p>Date management is a crucial skill in database development, especially when it comes to storing and retrieving data related to specific times. In MySQL, the DATE data type is essential for working with dates, not including the time of day. Through this article, you will learn how to efficiently handle DATE type data in MySQL, along with some essential functions that will allow you to manipulate, query and operate with dates effectively.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ffffff;color:#ffffff\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ffffff;color:#ffffff\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/nelkodev.com\/en\/blog\/master-dates-in-mysql-a-complete-guide-on-the-date-type\/#%C2%BFQue_es_el_Tipo_de_Dato_DATE_en_MySQL\" >What is the DATE Data Type in MySQL?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/nelkodev.com\/en\/blog\/master-dates-in-mysql-a-complete-guide-on-the-date-type\/#Ventajas_de_Usar_DATE\" >Advantages of Using DATE<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/nelkodev.com\/en\/blog\/master-dates-in-mysql-a-complete-guide-on-the-date-type\/#Funciones_Basicas_para_el_Manejo_de_DATE\" >Basic Functions for Managing DATE<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/nelkodev.com\/en\/blog\/master-dates-in-mysql-a-complete-guide-on-the-date-type\/#NOW\" >NOW()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/nelkodev.com\/en\/blog\/master-dates-in-mysql-a-complete-guide-on-the-date-type\/#CURDATE\" >CURDATE()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/nelkodev.com\/en\/blog\/master-dates-in-mysql-a-complete-guide-on-the-date-type\/#DATEDIFF\" >DATEDIFF()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/nelkodev.com\/en\/blog\/master-dates-in-mysql-a-complete-guide-on-the-date-type\/#DATE_ADD_y_DATE_SUB\" >DATE_ADD and DATE_SUB<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/nelkodev.com\/en\/blog\/master-dates-in-mysql-a-complete-guide-on-the-date-type\/#DAY_MONTH_YEAR\" >DAY(), MONTH(), YEAR()<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/nelkodev.com\/en\/blog\/master-dates-in-mysql-a-complete-guide-on-the-date-type\/#Manejo_Avanzado_de_DATE\" >Advanced DATE Management<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/nelkodev.com\/en\/blog\/master-dates-in-mysql-a-complete-guide-on-the-date-type\/#Extraccion_de_la_Semana_del_Ano\" >Week of the Year Extraction<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/nelkodev.com\/en\/blog\/master-dates-in-mysql-a-complete-guide-on-the-date-type\/#Formateo_de_Fechas\" >Date Formatting<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/nelkodev.com\/en\/blog\/master-dates-in-mysql-a-complete-guide-on-the-date-type\/#Comparaciones_de_Fecha\" >Date Comparisons<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/nelkodev.com\/en\/blog\/master-dates-in-mysql-a-complete-guide-on-the-date-type\/#Consideraciones_de_Rendimiento\" >Performance Considerations<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/nelkodev.com\/en\/blog\/master-dates-in-mysql-a-complete-guide-on-the-date-type\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFQue_es_el_Tipo_de_Dato_DATE_en_MySQL\"><\/span>What is the DATE Data Type in MySQL?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The DATE data type in MySQL is used to represent a date, without time information. Stores date in the format &#039;YYYY-MM-DD&#039;, where YYYY is the year, MM is the month, and DD is the day. An important feature of the DATE type is that it allows storing dates from &#039;1000-01-01&#039; until &#039;9999-12-31&#039;.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Ventajas_de_Usar_DATE\"><\/span>Advantages of Using DATE<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li><strong>Simplicity:<\/strong> By only containing date data, it is easy to make comparisons, ordering and calculations that do not require time precision.<\/li>\n<li><strong>Uniformity:<\/strong> The standard format &#039;YYYY-MM-DD&#039; avoids formatting confusion that can arise with other data types.<\/li>\n<li><strong>Storage Efficiency:<\/strong> The DATE type uses only 3 bytes of storage, which is efficient for databases with large volumes of date data.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Funciones_Basicas_para_el_Manejo_de_DATE\"><\/span>Basic Functions for Managing DATE<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To manipulate data of type DATE, MySQL offers a variety of functions that make it easy to extract and manipulate date information. Here I present some of the most used:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"NOW\"><\/span>NOW()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>This function returns the current date and time, being useful for automatic records of when an insert or update was made to the database.<\/p>\n<pre><code class=\"&quot;language-sql&quot;\">SELECT NOW();<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"CURDATE\"><\/span>CURDATE()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Unlike NOW(), CURDATE() returns only the current date.<\/p>\n<pre><code class=\"&quot;language-sql&quot;\">SELECT CURDATE();<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"DATEDIFF\"><\/span>DATEDIFF()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>If you need to calculate the difference in days between two dates, DATEDIFF() is your function. For example, to find out how many days have passed since a specific date until today:<\/p>\n<pre><code class=\"&quot;language-sql&quot;\">SELECT DATEDIFF(NOW(), &#039;2023-01-01&#039;) AS DaysDiff;<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"DATE_ADD_y_DATE_SUB\"><\/span>DATE_ADD and DATE_SUB<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To add or subtract a time range to a specific date, you can use these functions. For example, to add 10 days to the current date:<\/p>\n<pre><code class=\"&quot;language-sql&quot;\">SELECT DATE_ADD(CURDATE(), INTERVAL 10 DAY);<\/code><\/pre>\n<p>Or to subtract 3 months:<\/p>\n<pre><code class=\"&quot;language-sql&quot;\">SELECT DATE_SUB(CURDATE(), INTERVAL 3 MONTH);<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"DAY_MONTH_YEAR\"><\/span>DAY(), MONTH(), YEAR()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>These functions allow you to extract the day, month, and year of a date, respectively. They are particularly useful for breaking down a date into its components.<\/p>\n<pre><code class=\"&quot;language-sql&quot;\">SELECT DAY(&#039;2023-08-15&#039;), MONTH(&#039;2023-08-15&#039;), YEAR(&#039;2023-08-15&#039;);<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Manejo_Avanzado_de_DATE\"><\/span>Advanced DATE Management<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In addition to basic operations, there are more advanced techniques for working with dates in MySQL that can be extremely useful in more complex analyzes and generating reports.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Extraccion_de_la_Semana_del_Ano\"><\/span>Week of the Year Extraction<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code class=\"&quot;language-sql&quot;\">SELECT WEEK(&#039;2023-08-15&#039;);<\/code><\/pre>\n<p>This feature is useful for grouping data by week and performing weekly analysis.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Formateo_de_Fechas\"><\/span>Date Formatting<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The DATE_FORMAT function allows you to convert a date into a specific display format, which is especially useful for user interface or report generation where a particular date format is needed.<\/p>\n<pre><code class=\"&quot;language-sql&quot;\">SELECT DATE_FORMAT(NOW(), &#039;%W, %M %d %Y&#039;);<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Comparaciones_de_Fecha\"><\/span>Date Comparisons<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>You will often need to perform queries that involve date comparisons to filter records in a specific time range. For example, select all records created in the last year:<\/p>\n<pre><code class=\"&quot;language-sql&quot;\">SELECT * FROM your_table WHERE DATE &gt;= DATE_SUB(NOW(), INTERVAL 1 YEAR);<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Consideraciones_de_Rendimiento\"><\/span>Performance Considerations<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Efficient use of indexes on DATE type columns can significantly speed up querying large volumes of data. Be sure to index date columns that are frequently accessed by queries, especially in WHERE conditions, JOINs, or in groupings.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Mastering the DATE data type and related functions in MySQL enhances the ability to efficiently handle temporal information, which is indispensable in almost any modern database system. From activity logging to chronological event analysis, proper date manipulation allows developers and analysts to extract maximum value from their data.<\/p>\n<p>If you want to learn more about other aspects of MySQL or need specific help, feel free to visit <a href=\"https:\/\/nelkodev.com\/en\/\">my blog<\/a> o <a href=\"https:\/\/nelkodev.com\/en\/contact\/\">contact me<\/a>. I&#039;m here to help you on your path to database mastery.<\/p>","protected":false},"excerpt":{"rendered":"<p>El manejo de fechas es una habilidad crucial en el desarrollo de bases de datos, especialmente cuando se trata de almacenar y recuperar datos relacionados con tiempos espec\u00edficos. En MySQL, el tipo de dato DATE es esencial para trabajar con fechas, sin incluir la hora del d\u00eda. A trav\u00e9s de este art\u00edculo, aprender\u00e1s a manejar [&hellip;]<\/p>","protected":false},"author":1,"featured_media":29076,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[420,2185,2033],"tags":[205,500,2189,639,643,1473,646,358,48,352,74,645,2194,37],"class_list":["post-29075","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-curso-mysql","category-mysql-data-types","tag-blog","tag-completa","tag-curso","tag-data","tag-date","tag-domina","tag-fechas","tag-guia","tag-las","tag-mysql","tag-sobre","tag-tipo","tag-types","tag-una"],"_links":{"self":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/29075","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/comments?post=29075"}],"version-history":[{"count":0,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/29075\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media\/29076"}],"wp:attachment":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media?parent=29075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/categories?post=29075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/tags?post=29075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}