JRS: In article <11*********************@75g2000cwc.googlegroups.c om>,
dated Thu, 3 Aug 2006 03:07:57 remote, seen in
news:comp.lang.javascript,
bo***********@gmail.com posted :
>
In my ASP page, user can enter date in dd/mm/yyyy format. How can I use
Javascript to convert dd/mm/yyyy to yyyy-mm-dd hh:mm:ss.
Please give me some advices.
Before asking a question, one should read the frequently-cited newsgroup
FAQ. It contains the words "date" "dates" "time" "times" in Section 3.
If you mean exactly what you have written above, then all you need is
indicated by
St = "03/08/2006"
St = St.replace(/(\d\d).(\d\d).(\d\d\d\d)/, "$3-£2-$1 00:00:00")
// 2006-08-03 00:00:00
Given that your users can handle dd/mm/yyyy, I assume that they are too
intelligent to use the 12-hour clock for data.
If you want to read that format into a Date Object,
D = new Date(St.replace(/(\d\d).(\d\d).(\d\d\d\d)/, "$3/$2/$1"))
should always work though not guaranteed by ECMA spec; and the following
is so guaranteed.
M = St.match(/(\d\d).(\d\d).(\d\d\d\d)/)
D = new Date(M[3], M[2]-1, M[1])
( Alternatively, consider
D = new Date(St.split('/').reverse().join('/'))
and note that JD's method using split will, coded as shown, give the
corresponding date in the following month. I test my code. )
If the input is not GUARANTEED ##/##/####, then M should be checked for
not null before the D line.
If the input may be an invalid date such as 01/17/2006, then validate it
as via sig line 3 below.
If you want to output a Date Object in ISO format, then add & use Method
ISOlocalDTstr, or use it to guide the creation of a function.
The above uses a Date Object representing the input as a local date.
Extended organisations should consider the use of UTC/GMT. The UTC
Methods of Date Objects are much faster than the local ones.
Read the newsgroup FAQ.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.