Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Date question

0 views
Skip to first unread message

Xerxes

unread,
Aug 30, 2010, 5:33:42 PM8/30/10
to
Hi,
I am trying to convert a date that I get from DB in the "Thu Aug 26
00:00:00 EDT 2010" format to mm/dd/yyyy.
I use d.format("mm/dd/yyyy") but I get 00/26/2010. What am I doing
wrong?

Ivan Marsh

unread,
Aug 30, 2010, 5:36:06 PM8/30/10
to
Xerxes wrote:

Without a look at the code in question it's going to be difficult to guess
but I'd say you're parsing the date incorrectly.

--
Please stop misusing the term conservative.

SAM

unread,
Aug 30, 2010, 7:15:21 PM8/30/10
to
Le 30/08/10 23:33, Xerxes a écrit :

javascript:
var d = new Date('Thu Aug 26 00:00:00 EDT 2010');
var t = [d.getMonth(), d.getDate(), d.getFullYear()];
if(t[0]<10) t[0] = '0'+t[0];
if(t[1]<10) t[1] = '0'+t[1];
alert(t.join('/'));

07/26/2010


--
Stéphane Moriaux avec/with iMac-intel

RobG

unread,
Aug 30, 2010, 7:24:51 PM8/30/10
to
On Aug 31, 7:33 am, Xerxes <mfkash...@gmail.com> wrote:
> Hi,
> I am trying to convert a date that I get from DB in the "Thu Aug 26
> 00:00:00 EDT 2010" format to mm/dd/yyyy.

That format will likely be misinterpreted by 95% of the world's
population where the date is in the range 1 to 12. Consider using
something less ambiguous.


> I use d.format("mm/dd/yyyy") but I get 00/26/2010. What am I doing
> wrong?

The ECMAScript Date object doesn't have a format method, so I'll guess
that you're using a format function written by someone else.

Without knowing the function you are using or seeing the code,
comments about what you might be "doing wrong" are no more than
conjecture. Posting the code for the function you are trying to use,
or a link to it, may provoke a more useful answer.


--
Rob

SAM

unread,
Aug 30, 2010, 8:27:10 PM8/30/10
to
Le 31/08/10 01:24, RobG a écrit :

> On Aug 31, 7:33 am, Xerxes<mfkash...@gmail.com> wrote:
>> Hi,
>> I am trying to convert a date that I get from DB in the "Thu Aug 26
>> 00:00:00 EDT 2010" format to mm/dd/yyyy.
>
> That format will likely be misinterpreted by 95% of the world's
> population where the date is in the range 1 to 12.

What does that mean "range 1 to 12" ?
of what are you speaking ?

RobG

unread,
Aug 30, 2010, 8:48:27 PM8/30/10
to
On Aug 31, 10:27 am, SAM <stephanemoriaux.NoAd...@wanadoo.fr.invalid>
wrote:

The day number of the month.


--
Rob

Jukka K. Korpela

unread,
Aug 30, 2010, 11:13:56 PM8/30/10
to
SAM wrote:

> var d = new Date('Thu Aug 26 00:00:00 EDT 2010');
> var t = [d.getMonth(), d.getDate(), d.getFullYear()];
> if(t[0]<10) t[0] = '0'+t[0];
> if(t[1]<10) t[1] = '0'+t[1];
> alert(t.join('/'));
>
> 07/26/2010

So it does not get the month right. The Date object contains the month
number as starting from zero, so you need to t[0]++ before the if
statements, for example.

--
Yucca, http://www.cs.tut.fi/~jkorpela/

SAM

unread,
Aug 31, 2010, 3:49:53 AM8/31/10
to
Le 31/08/10 02:48, RobG a écrit :

Ha! OK, so it's not the 'date'

and of course I fell into the trap :-(

SAM

unread,
Aug 31, 2010, 3:50:14 AM8/31/10
to
Le 31/08/10 05:13, Jukka K. Korpela a écrit :

Oooops ! August isn't 07 ? and never anybody told it to me ?

var t = [d.getMonth()+1, d.getDate(), d.getFullYear()];

javascript:


var d = new Date('Thu Aug 26 00:00:00 EDT 2010');

var t = [d.getMonth()+1, d.getDate(), d.getFullYear()];


if(t[0]<10) t[0] = '0'+t[0];
if(t[1]<10) t[1] = '0'+t[1];
alert(t.join('/'));

--
Stéphane Moriaux avec/with iMac-intel

Thomas 'PointedEars' Lahn

unread,
Aug 31, 2010, 3:14:52 PM8/31/10
to
Jukka K. Korpela wrote:

> SAM wrote:
>> var d = new Date('Thu Aug 26 00:00:00 EDT 2010');

Don't. Passing string values to the Date constructor is inherently
unreliable.

>> var t = [d.getMonth(), d.getDate(), d.getFullYear()];

An Object instance would be easier to maintain:

var t = {
year: d.getFullYear(),
month: d.getMonth(),
date: d.getDate()
};

But either one is overkill without a formatting function that does

>> if(t[0]<10) t[0] = '0'+t[0];
>> if(t[1]<10) t[1] = '0'+t[1];

For example:

/* or use the more flexible variant that I recommended shortly ago */
function leadingZero(n)
{
return (n < 10) ? "0" + n : n;
}

var t = {
year: d.getFullYear(),
month: d.getMonth(),
date: d.getDate(),

toString: function() {
return [leadingZero(this.month),
leadingZero(this.date),
this.year].join("/");
}
};

>> alert(t.join('/'));

window.alert(t);

>> 07/26/2010
>
> So it does not get the month right. The Date object contains the month
> number as starting from zero, so you need to t[0]++ before the if
> statements, for example.

For *this* example it would be better to add one in the first place:

var t = {
month: d.getMonth() + 1,
date: d.getDate(),
year: d.getFullYear()
};


PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300...@news.demon.co.uk> (2004)

SAM

unread,
Aug 31, 2010, 4:33:48 PM8/31/10
to
Le 31/08/10 21:14, Thomas 'PointedEars' Lahn a écrit :

> Passing string values to the Date constructor is inherently
> unreliable

Ha ?

SAM

unread,
Aug 31, 2010, 4:39:05 PM8/31/10
to
Le 31/08/10 21:14, Thomas 'PointedEars' Lahn a écrit :
>
>> SAM wrote:
>
>>> var t = [d.getMonth(), d.getDate(), d.getFullYear()];
>
> An Object instance would be easier to maintain:

maybe
but I wanted the shortest

and ... there ... what is to "maintain" ?

(...)
>>> alert(t.join('/'));
>
> window.alert(t);

"window" isn't it intrinsic ?

Dr J R Stockton

unread,
Aug 31, 2010, 3:49:43 PM8/31/10
to
In comp.lang.javascript message <b346a950-c2aa-40c2-9d2c-fee757bba96d@t2
g2000yqe.googlegroups.com>, Mon, 30 Aug 2010 14:33:42, Xerxes
<mfka...@gmail.com> posted:

As at about 2010-08-31 19:25 UTC, AFAICS no-one has really attempted to
answer the explicit question.

Since the example date is in a (ludicrous) format commonly used in US
software and that format has been readable by JScript new Date(" ...
") for longer than I have been using JavaScript, it is safe to expect
that no browser will stop accepting it, with the desired meaning.
However, although the ECMA standard requires new Date(String) to
work, it does not specify any string formats.

If you have read the string into a Date Object, you can check the input
stage by using its default .toString method.

Opera 9.50 did not accept alphabetical local time offset designators,
which are intrinsically ambiguous. Opera 10.61 does, which is a pity.
I don't recall exactly when Opera changed.


What you did wrong :

You did not give the browser and OS versions; they often matter,
particularly with date strings.

You did not give a complete executable (by copy'n'paste without editing)
routine showing the problem and including ALL code including library
code.

You apparently did not try to locate the problem by examining the
intermediate value more directly.

You are apparently using some form of date library.

You are trying to generate a format which is not that recommended by ISO
8601, ANSI X3.30-1985(R1991), FIPS PUB 4-1 & 4-2, CSA Z234.5:1989.

You did not say that you had read the newsgroup FAQ.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.

Xerxes

unread,
Sep 1, 2010, 12:23:39 PM9/1/10
to
On Aug 31, 4:39 pm, SAM <stephanemoriaux.NoAd...@wanadoo.fr.invalid>
wrote:

This is what I have:

var d = makeDate(record["StartDate"]); // record["StartDate"] is date
from database, in the 2010-08-31 06:00:00.000 format
tbStart.value = d.format("mm/dd/yyyy");

function makeDate(oDate) {
var datetimeObj = oDate.split(" ");
var dateObj = datetimeObj[0];
var timeObj = datetimeObj[1];

var date = dateObj.split("/");
//Date(year, month, day, hours, minutes, seconds, milliseconds);
var year = parseInt(date[2]);
var month = parseInt(date[0]) - 1;
var day = parseInt(date[1]);
var selectedDate = new Date(year, month, day);
return selectedDate; // This returns Sat Mar 1 00:00:00 EST 2008
}

After a call to this function, d.format("mm/dd/yyyy") returns
00/01/2008.

I have to do this because I have a calendar object that has setDate(d)
client side function that expects a javascript Date() parameter

I hope this helps.

Xerxes

unread,
Sep 1, 2010, 12:59:53 PM9/1/10
to
On Aug 30, 7:24 pm, RobG <rg...@iinet.net.au> wrote:

Even when I execute d = new Date(2008,3,1), I get Tue Apr 1 00:00:00
EDT 2008. Is this normal format?

SAM

unread,
Sep 1, 2010, 2:03:37 PM9/1/10
to
Le 01/09/10 18:23, Xerxes a écrit :

>
> This is what I have:
>
> var d = makeDate(record["StartDate"]); // record["StartDate"] is date
> from database, in the 2010-08-31 06:00:00.000 format


Probably something like :
var d = makeDate(<?php echo record["StartDate"] ?>);
so
record["StartDate"]
will be the string
2010-08-31 06:00:00.000

then, date in mm/dd/yyyy :
d = d.substring(0,d.indexOf(' ')).split('-');
tbStart.value = d[1]+'/'+d[2]+'/'+d[0]

Date in dd/mm/yyyy :
d = d = d.substring(0,d.indexOf(' ')).split('-').reverse().join('/');
alert(d);


function makrDate(d) {
d = d.substr(0,10).split(/[^\d]/);
d = new Date(d[1]-1+','+d[2]+','+d[0]);
return d;
}

alert(makrDate('2010-08-31 06:00:00.000'));
alert(makrDate('2010-08-05 06:00:00.000'));
alert(makrDate('2010-8-2 06:00:00.000'));
alert(makrDate('2010-8-2'));
alert(makrDate('2010:8:2 bla bla bla'));


> tbStart.value = d.format("mm/dd/yyyy");
>
> function makeDate(oDate) {
> var datetimeObj = oDate.split(" ");
> var dateObj = datetimeObj[0];
> var timeObj = datetimeObj[1];
>
> var date = dateObj.split("/");

wrong separator, no ?

var date = dateObj.split("-");
or
var date = dateObj.split(/[\/-]/); // separator = / or -
or
var date = dateObj.split(/[^\d]/); // separator != number

> //Date(year, month, day, hours, minutes, seconds, milliseconds);
> var year = parseInt(date[2]);
> var month = parseInt(date[0]) - 1;
> var day = parseInt(date[1]);
> var selectedDate = new Date(year, month, day);
> return selectedDate; // This returns Sat Mar 1 00:00:00 EST 2008
> }
>
> After a call to this function, d.format("mm/dd/yyyy") returns
> 00/01/2008.

It would have been interested to get what the browser receives as script
javascript (in particular for d)

SAM

unread,
Sep 1, 2010, 2:10:25 PM9/1/10
to
Le 01/09/10 18:59, Xerxes a écrit :

>
> Even when I execute d = new Date(2008,3,1), I get Tue Apr 1 00:00:00
> EDT 2008. Is this normal format?

I get :
Tue Apr 01 2008 00:00:00 GMT+0200 (CET)
here in France with my Firefox

new Date(fullyear, month-number -1, day-number)

Dr J R Stockton

unread,
Sep 2, 2010, 5:21:07 PM9/2/10
to
In comp.lang.javascript message <3f9f6002-635f-4223-a3e5-279fc9362a25@g1
7g2000yqe.googlegroups.com>, Wed, 1 Sep 2010 09:23:39, Xerxes
<mfka...@gmail.com> posted:

>This is what I have:
>
>var d = makeDate(record["StartDate"]); // record["StartDate"] is date
>from database, in the 2010-08-31 06:00:00.000 format
>tbStart.value = d.format("mm/dd/yyyy");
>
>function makeDate(oDate) {
> var datetimeObj = oDate.split(" ");
> var dateObj = datetimeObj[0];
> var timeObj = datetimeObj[1];
>
> var date = dateObj.split("/");
> //Date(year, month, day, hours, minutes, seconds, milliseconds);
> var year = parseInt(date[2]);
> var month = parseInt(date[0]) - 1;
> var day = parseInt(date[1]);
> var selectedDate = new Date(year, month, day);
> return selectedDate; // This returns Sat Mar 1 00:00:00 EST 2008
>}
>
>After a call to this function, d.format("mm/dd/yyyy") returns
>00/01/2008.


Read the newsgroup FAQ on the subject of parseInt to understand your
problem.

Your code is largely unnecessary. Consider :

S = "2010-08-31 06:00:00.000" // a fixed standard format
S = S.replace(/-/g, "/").substring(0, 19) + " UTC"
D = new Date(S)
or
D = new Date(S.replace(/(....).(..).(...........).*/, "$1/$2/$3 GMT"))

Note that your example date string has no offset specifier; I have
assumed it to be UTC. Future browsers should not need the .replace and
perhaps not the .substring, having a routine to accept that form
directly. Current Opera already does not need the .replace.

Much date/time work is best done using UTC exclusively. Use local dates
only if time offset matters.

0 new messages