How to convert datetime format using Javascript

B

bonnie.tangyn

Dear all

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.

Cheers
Bon
 
R

RobG

Dear all

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.

You can try using a date object:

new Date('dd/mm/yyyy');

however on my system 12/02/2005 always returns 2-Dec-2005 regardless of
how I set my system or browser date format preferences. It seems the
absurd US date system is all pervasive.

If you do not provide a time, the above conversion of string to date
object will always return 00:00:00 (at least on the systems/browsers I
tested). Given the vaguaries of how the string will be interpreted if
converted to a date object, and the fact that the time seems to be
irrelevant, you might as well just use string manipulation:

<form action="">
Date (dd/mm/yyyy) <input type="text" name="val1" size="30"
value="12/02/1005"><br>
<input type="button" value="test"
onclick="
var dateBits = this.form.val1.value.split('/');
alert(dateBits[2] + '-' + dateBits[1]
+ '-' + dateBits[0] + ' 00:00:00');
">
</form>


You might want to validate the input, add leading zeros, etc.
 
R

RobG

Jim said:
Dear all

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.

Assuming that the date format as dd/mm/yyyy you can easily convert it to a
date by splitting it into date parts:

myDateParts = myDate.split("/");

You can then build a new date from the parts like so:

myJSDate = new Date(myDateParts[2], myDateParts[1], myDateParts[0]);

Except that the month number starts at zero, so the above will be out
by 1 month. Subtract 1 from the month number:

myJSDate = new Date(myDateParts[2], myDateParts[1] - 1,
myDateParts[0]);

using the same array but I always prefer generating a real date. Note that
you're not specifying a time so time will always be "00:00:00".

Except that it appears to be unnecessary and may introduce a complexity
that isn't required (two easy mistakes are in this thread already).
What is the benefit of using a date object when all that is required is
re-formatting a string?

[...]
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Thu, 3 Aug 2006 03:07:57 remote, seen in
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.
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Thu, 3 Aug 2006 03:44:04 remote, seen in
news:comp.lang.javascript said:
new Date('dd/mm/yyyy');
If you do not provide a time, the above conversion of string to date
object will always return 00:00:00 (at least on the systems/browsers I
tested).

Strictly speaking, never, since that code creates a Date Object which
holds an IEEE Double of GMT milliseconds rather than a String. It is
the toString method of the Date Object (maybe called implicitly) or the
other non-UTC methods which are responsible for the "00:00:00"; and the
UTC methods will generally give a different result.

If the date in question is the last Sunday of March or October, and the
system is set for an EU location in the time zone West of Greenwich (the
Azores and South-East Greenland), then I'd be reluctant to predict the
result.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Thu, 3 Aug 2006 11:06:49 remote, seen in Jim
Davis said:
Part of this is are "dateFormat()" and "timeFormat()" methods. You can use
them to output the format you want like so:

myJSDate.formatDate("yyyy-mm-dd") + " " + myJSDate.timeFormat("hh:mm:ss")

Would it not be better if the format methods allowed spaces in their
argument strings? It would then not be necessary to use the middle
string above.

Suggestion : any alphabetic "word" will be interpreted either as a
recognised component or as an error; any non-alphanumeric will be taken
as a literal to be copied; decimal digits need further thought; there
could be an escape character.

Read the newsgroup FAQ.
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top