Date question

X

Xerxes

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?
 
S

SAM

Le 30/08/10 23:33, Xerxes a écrit :
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?

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
 
R

RobG

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.
 
S

SAM

Le 31/08/10 01:24, RobG a écrit :
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 ?
 
J

Jukka K. Korpela

SAM said:
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.
 
S

SAM

Le 31/08/10 05:13, Jukka K. Korpela a écrit :
SAM said:
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.

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('/'));
 
T

Thomas 'PointedEars' Lahn

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
 
D

Dr J R Stockton

In comp.lang.javascript message <b346a950-c2aa-40c2-9d2c-fee757bba96d@t2
g2000yqe.googlegroups.com>, Mon, 30 Aug 2010 14:33:42, Xerxes
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?

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.
 
X

Xerxes

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" ?

(...)
   window.alert(t);

"window" isn't it intrinsic ?

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.
 
X

Xerxes

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.


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.

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

SAM

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)
 
S

SAM

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)
 

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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top