472,142 Members | 1,350 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,142 software developers and data experts.

How to display date in DD-MM-YYYY format?

157 100+
Helo,

I have a calendar on my webpage on which the date format is 'DD-MM-YYYY'; user selects a date and it has to be stored in the same format in the backend; but I checked in MySQL there is 'Date' which has the format 'YYYY-MM-DD'.

Is there any way I can store the date in the 'DD-MM-YYYY' or just 'DD-MM-YY' format?
Feb 15 '07 #1
9 70391
ronverdonk
4,258 Expert 4TB
Make it a VARCHAR(10) or a CHAR(10) field and store it in your format.

Ronald :cool:
Feb 16 '07 #2
abctech
157 100+
Make it a VARCHAR(10) or a CHAR(10) field and store it in your format.

Ronald :cool:
Thank you,

Actually I need to sort this date coumn and fire queries like select the records between date so and so or select all records for month of Jan etc etc..if I make it as Varchar or Char instead of Date will I still be able to do this? Please guide.
Feb 16 '07 #3
ronverdonk
4,258 Expert 4TB
No, all date calculations are done using the YYYY-MM-DD format (MySQL DATE format).

I would advise you to leave the DATE format in the db as it is, so you can do calculations, searches etc. Only re-format the date to your required format when displaying it to the user using the DATE_FORMAT MySQL command:
Expand|Select|Wrap|Line Numbers
  1. DATE_FORMAT(your_date, "%d-%e-%Y")
Ronald :cool:
Feb 16 '07 #4
abctech
157 100+
No, all date calculations are done using the YYYY-MM-DD format (MySQL DATE format).

I would advise you to leave the DATE format in the db as it is, so you can do calculations, searches etc. Only re-format the date to your required format when displaying it to the user using the DATE_FORMAT MySQL command:
Expand|Select|Wrap|Line Numbers
  1. DATE_FORMAT(your_date, "%d-%e-%Y")
Ronald :cool:
Thanks for your suggestion, it should solve my difficulty except for one problem, the Calendar that I'm using(here is the link for it ) has only limited Date-formats, currently I'm using the DD-MM-YYYY format and when I try to save the date in this format it is incorrectly saved, for eg. if I try to save 17-2-2007 it gets saved in the backend 'DATE' column as 2012-02-20, can anything be done through MySQl to avoid this?

I really need the user to choose a Date in the DD-MM-YYYY format only.
Feb 16 '07 #5
ronverdonk
4,258 Expert 4TB
To store your DD_MM-YYYY date, use the MySQL STR_TO_DATE() function. Example:
Expand|Select|Wrap|Line Numbers
  1. STR_TO_DATE('31/07/2006', '%d/%m/%Y');
returns 2006-07-31.

See the MySQL Date and Time functions

Ronald :cool:
Feb 16 '07 #6
abctech
157 100+
To store your DD_MM-YYYY date, use the MySQL STR_TO_DATE() function. Example:
Expand|Select|Wrap|Line Numbers
  1. STR_TO_DATE('31/07/2006', '%d/%m/%Y');
returns 2006-07-31.

See the MySQL Date and Time functions

Ronald :cool:
I tried the above query but I get the message "You have an error in your SQL syntax". I am using MySQL version 4.0.16 and I searched the entire manual on my comp for the STR_TO_DATE() function, couldn't locate it, does this function exist in the previous versions or could there be some other problem?
Feb 17 '07 #7
ronverdonk
4,258 Expert 4TB
Firstly: what I showed was not a query. And of course you cannot execute it. It showed the way you can use the STR_TO_DATE function. In order to use it proper you must put it in an SQL command like SELECT, UPDATE, etc.

Secondly: I cannot imagine that you cannot find that function. When you just follow the link I showed, you will be linked to the date and time functions chapter in the MySQL manual. Within that chapter you can search for the function you want to find.

Ronald :cool:
Feb 17 '07 #8
abctech
157 100+
Firstly: what I showed was not a query. And of course you cannot execute it. It showed the way you can use the STR_TO_DATE function. In order to use it proper you must put it in an SQL command like SELECT, UPDATE, etc.

Secondly: I cannot imagine that you cannot find that function. When you just follow the link I showed, you will be linked to the date and time functions chapter in the MySQL manual. Within that chapter you can search for the function you want to find.

Ronald :cool:
Yes I did use the function in a query(SELECT) and I was getting a 'syntax-error'. I also visited the page the link for which you'd provided and read the STR_TO_DATE() function there but after trying to use it I was getting an error. I searched for the same in the MySQL manual on my computer, couldn't find it anywhere. I have DATE_FORMAT(), TIME_FORMAT(), MAKEDATE(), NOW(), CAST() etc etc but no STR_TO_DATE() :(.
I really dont know why that is, perhaps my MySQL version doesn't have this function hence I'm getting the error.

Please guide if there is some other way. For the time being I'm trying to convert the DD-MM-YYYY Date into YYYY-MM-DD Date through the front-end(Java) itself and then save it in the backend.
Feb 17 '07 #9
abctech
157 100+
Helo again,

I got it resolved now, tried the program using MySQl 5.0, the STR_TO_DATE() works there.Thanks for all your help, much appreciated!
Feb 17 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Franz | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.