Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i declare time variables of the form XX:YY and date variables of the form
dd-mm-yyyy in MySQL?

What I have tried:

I'm working on a project about a cinema database and I want to create date and time variables for the start of a movie broadcast
Posted
Updated 7-Aug-19 6:22am
Comments
Richard MacCutchan 7-Aug-19 12:06pm    
Do not store dates and times as text strings. Use the proper data types as described in the documentation.

Actually you cannot; as Date and Time types in most RDBMS systems are actually numbers.
It is up to whatever program you are using to view the information to actually format it into a human-recognizable format at the presentation level.

The way to do this is to present this is to use the Date_Format() which will convert the Date/Time into as string.

Reference:
[1]: MySQL :: MySQL 8.0 Reference Manual :: 12.7 Date and Time Functions[^]
 
Share this answer
 
GIYF: MySQL :: MySQL 8.0 Reference Manual :: 11.3 Date and Time Types[^]
If you insist on "dd-mm-yyyy' format, which may be ambiguous if your users expect "mm-dd-yyyy', you may have to google some more to see how to format dates appropriately. I'd suggest you stick with "yyyy-mm-dd", its unambiguous, and as a bonus, if you need (or want) to sort by date in the app, as opposed to retrieving sorted data from the database, a straight string compare works correctly, without having to write a method to decompose and compare the dates.
 
Share this answer
 
Comments
Member 14549747 7-Aug-19 11:22am    
Yes "yyyy-mm-dd" . When you say string compare what do you mean ?
k5054 7-Aug-19 12:41pm    
If we have a function compare(string1, string2) then compare("2018-08-01", "2019-07-02") will show the first string is less than the second. That's probably what we want. If we use "mm-dd-yyyy",then compare("08-01-2018", "07-02-2019") shows the first string is greater than the second, which is probably not what we want. If we wanted to compare dates using "mm-dd-yyyy", we'd have to write a compare_date(date1, date2) function that extracts, year, month, day from each string and then compares the years, months, and days to determine if date1 is less than date2.
Member 14549747 7-Aug-19 14:58pm    
Thank you very much .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900