Date problem

R

Robert Bravery

HI all,

I'm new toJS.
I am trying to get a user inputed date into mysql. The mysql database
accepts the date in yyyy-mm-dd format.
The user, from South Africa inputs the date in a web form in dd/mm/yyyy
format. Obviously when this is stored to the database table, the dates are
not quite what one would expect. I'm wantng to change the date format to
that what mysql would accept before it gets posted. I am looking for some
help with a script that would be able to do this for me. I'm having great
dificulty in getting this right


Thanks

Robert
 
T

Thomas 'PointedEars' Lahn

Robert said:
I am trying to get a user inputed date into mysql. The mysql database
accepts the date in yyyy-mm-dd format.
The user, from South Africa inputs the date in a web form in dd/mm/yyyy
format.

Tell him he should not, or make the form or the error message more obvious.
This is required because you want it to work without client-side script
support, too.


PointedEars
 
I

Ian Collins

Thomas said:
Robert Bravery wrote:




Tell him he should not, or make the form or the error message more obvious.
This is required because you want it to work without client-side script
support, too.
Why shouldn't he? He is the user and dd/mm/yyyy is the natural form of
dates in a large percentage of the world.

Accept what the user wants and convert to what the database expects.
 
B

Bart Van der Donck

Robert said:
I'm new toJS.
I am trying to get a user inputed date into mysql. The mysql database
accepts the date in yyyy-mm-dd format.
The user, from South Africa inputs the date in a web form in dd/mm/yyyy
format. Obviously when this is stored to the database table, the dates are
not quite what one would expect. I'm wantng to change the date format to
that what mysql would accept before it gets posted. I am looking for some
help with a script that would be able to do this for me. I'm having great
dificulty in getting this right

I'ld counsel a SQL approach:

INSERT INTO myTable
VALUES (
CONCAT (
MID('28/05/2006',7,4),
MID('28/05/2006',4,2),
MID('28/05/2006',1,2)
)
);

Note that you'ld need to guarantee the dd/mm/yyyy format in this
scenario. In this code, 'myTable' has only one column that is of the
DATE-type.

Hope this helps,
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Mon, 29 May
2006 21:53:47 remote, seen in Robert Bravery
I am trying to get a user inputed date into mysql. The mysql database
accepts the date in yyyy-mm-dd format.
The user, from South Africa inputs the date in a web form in dd/mm/yyyy
format.

Converting from UK to ISO format, presuming the input will always be 10
characters dd/mm/yyyy, using javascript :-

S = "12/05/2007"
S = S.replace(/(..).(..).(....)/, "$3-$2-$1")
// S now is 2007-05-12


Converting from UK to ISO format, not presuming the input will always be
10 characters dd/mm/yyyy but will contain separated digits for D M Y,
using javascript :-

S = "12/09/2007"
S = S.replace(/\D*(\d+)\D+(\d+)\D+(\d+)\D*/, "20$3-0$2-0$1").
replace(/.*(\d\d\d\d)-.*(\d\d)-.*(\d\d)/, "$1-$2-$3")

The first is safe enough, and fits your case; the second may need more
thought (it assumes two-digit years are in the now-current centade).

Neither check whether the date is numerically OK.

I make no comment on whether it is sensible for you to use javascript to
do this.

Before posting to a newsgroup, you should seek and read its FAQ; see
below.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,160
Latest member
CollinStri
Top