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

How to format datestring to yyyy-mm-dd?

6 views
Skip to first unread message

LarryM

unread,
May 8, 2003, 5:23:52 AM5/8/03
to
Hi,

I have a Calendar (web) that returns a date value
"Thu Jul 25 00:00:00 UTC+0200 2002".

Is this format understood by javascript and if so, is there
a javascript function to format this to "yyyy-mm-dd", that
is "2002-07-25"?

Thanks,
Larry

Adrien ESPARRON

unread,
May 8, 2003, 6:44:16 AM5/8/03
to

"LarryM" <lm1...@telia.com> a écrit dans le message de news:
6h8kbvoi81m89t83o...@4ax.com...

Hi !

A good question for Dr. John Stockton !!! Your calendar output is not only
"understood" by JavaScript, but it seems to be "pure" JavaScript. Test the
following :

<html>
<head>
<title></title>
</head>
<body>
<script language="JavaScript">
now = new Date();
localtime = now.toString();
document.write(localtime);
</script>
</body>
</html>

The output is exactly what you describe. So, if you have the possibility to
rewrite the script, use something like the following (french variable names,
but very easy to adapt your needs) :

<html>
<head>
<title></title>
<script type="text/javascript">
function date() {
moiss = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10',
'11','12'];
maintenant = new Date();
jour = maintenant.getDate();
if(jour < 10 ) { jour = '0' + jour }
mois = moiss[maintenant.getMonth()];
annee = maintenant.getFullYear();
document.write(annee + "-" + mois + "-" + jour);
}
</script>
</head>
<body onLoad="date()";>
</body>
</html>

So far, enjoy !

Adrien


LarryM

unread,
May 8, 2003, 10:33:23 AM5/8/03
to

Hi,
thanks Adrian.

It seems like your function uses the current timevalue
('maintenant = new Date();').
But I already have the string (or date) "Thu Jul 25 00:00:00
UTC+0200 2002", and just want to reformat it.
I suppose that the string should be passed into your
function date(), or should we call it fuDate().

In the meantime I have treated the string, and by substrings
taken out the new string "2002-07-25". (Yes, you have to
transform Jul etc to 07 etc).
So maybe in a clumsy way I have got what I wanted.

But maybe it would be nice to see what this function should
have looked like if you already have this certain
date-string which you want to format to 'YYYY-MM-DD'?

Larry

Adrien ESPARRON

unread,
May 8, 2003, 1:26:33 PM5/8/03
to

> Hi,
> thanks Adrian.
>
> It seems like your function uses the current timevalue
> ('maintenant = new Date();').
> But I already have the string (or date) "Thu Jul 25 00:00:00
> UTC+0200 2002", and just want to reformat it.
> I suppose that the string should be passed into your
> function date(), or should we call it fuDate().
>
> In the meantime I have treated the string, and by substrings
> taken out the new string "2002-07-25". (Yes, you have to
> transform Jul etc to 07 etc).
> So maybe in a clumsy way I have got what I wanted.

You are welcome ! Only some ideas from my side, and happy to see that you
could treat the string, reformatting it. It was the original purpose, I know
and understood it. Could be however fair to show everybody how you did :
it's not only your problem and my interest, but a general purpose. Thanks in
advance !

>
> But maybe it would be nice to see what this function should
> have looked like if you already have this certain
> date-string which you want to format to 'YYYY-MM-DD'?
>

Just ask the web author to replace his function with mine !

Regards, Adrien


LarryM

unread,
May 8, 2003, 2:42:10 PM5/8/03
to
On Thu, 8 May 2003 19:26:33 +0200, "Adrien ESPARRON"
<_ave...@libertysurf.fr> wrote:

>
>> Hi,
>> thanks Adrian.
>>
>> It seems like your function uses the current timevalue
>> ('maintenant = new Date();').
>> But I already have the string (or date) "Thu Jul 25 00:00:00
>> UTC+0200 2002", and just want to reformat it.
>> I suppose that the string should be passed into your
>> function date(), or should we call it fuDate().
>>
>> In the meantime I have treated the string, and by substrings
>> taken out the new string "2002-07-25". (Yes, you have to
>> transform Jul etc to 07 etc).
>> So maybe in a clumsy way I have got what I wanted.
>
>You are welcome ! Only some ideas from my side, and happy to see that you
>could treat the string, reformatting it. It was the original purpose, I know
>and understood it. Could be however fair to show everybody how you did :
>it's not only your problem and my interest, but a general purpose. Thanks in
>advance !

ok, since you ask.
A month-array would have been nicer.
But sometimes I am just rather safe then sorry and go for a
mentally simple solution:

So, inDateStr has a fix format:
"Thu Jul 25 00:00:00 UTC+0200 2002".
Differs only on the DayNo which can be 1 or 2 digits (no
preceeding zeros).

function fuDateFormat(inDateStr){
var datum= new String(inDateStr);
var yearNo;
var monthNo;
var dayNo;

var monthName = datum.substring(4,7);
if (monthName=="Jan") {
monthNo="01";
}
else if (monthName=="Feb") {
monthNo="02";
}
else if (monthName=="Mar") {
monthNo="03";
}
else if (monthName=="Apr") {
monthNo="04";
}
else if (monthName=="May") {
monthNo="05";
}
else if (monthName=="Jun") {
monthNo="06";
}
else if (monthName=="Jul") {
monthNo="07";
}
else if (monthName=="Aug") {
monthNo="08";
}
else if (monthName=="Sep") {
monthNo="09";
}
else if (monthName=="Oct") {
monthNo="10";
}
else if (monthName=="Nov") {
monthNo="11";
}
else if (monthName=="Dec") {
monthNo="12";
}
if (datum.length==32) { //1 digit day
dayNo="0"+datum.substring(8,9);
yearNo=datum.substring(28);
}
else { // 2 digits day
dayNo=datum.substring(8,10);
yearNo=datum.substring(29);
}
var datestr=yearNo+"-"+monthNo+"-"+dayNo;
return datestr;
}

have a nice day!
LArry

Dr John Stockton

unread,
May 8, 2003, 3:27:42 PM5/8/03
to
JRS: In article <6h8kbvoi81m89t83o...@4ax.com>, seen in
news:comp.lang.javascript, LarryM <lm1...@telia.com> posted at Thu, 8
May 2003 09:23:52 :-

>
>I have a Calendar (web) that returns a date value
>"Thu Jul 25 00:00:00 UTC+0200 2002".

Then you seem to be Continental.

>Is this format understood by javascript and if so, is there
>a javascript function to format this to "yyyy-mm-dd", that
>is "2002-07-25"?

It is, but you may not need to use that.

These add a new Method to the Date Object; don't re-parse unnecessarily.

function LZ(x) {return(x<0||x>9?"":"0")+x}

Date.prototype.ISOlocaldateStr =
new Function(" /* Date.ISOlocaldateStr */ with (this)\n return " +
"getFullYear()+'-'+LZ(getMonth()+1)+'-'+LZ(getDate())")

// then

D = new Date("Thu Jul 25 00:00:00 UTC+0200 2002")
D.ISOlocaldateStr() // --> 2002-07-24
D // --> Wed Jul 24 23:00:00 UTC+0100 2002

See below.

--
© John Stockton, Surrey, UK. j...@merlyn.demon.co.uk Turnpike v4.00 MSIE 4 ©
<URL:http://www.jibbering.com/faq/index.html> FAQ for comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

LarryM

unread,
May 9, 2003, 6:46:50 AM5/9/03
to
On Thu, 8 May 2003 20:27:42 +0100, Dr John Stockton
<sp...@merlyn.demon.co.uk> wrote:

>JRS: In article <6h8kbvoi81m89t83o...@4ax.com>, seen in
>news:comp.lang.javascript, LarryM <lm1...@telia.com> posted at Thu, 8
>May 2003 09:23:52 :-
>>
>>I have a Calendar (web) that returns a date value
>>"Thu Jul 25 00:00:00 UTC+0200 2002".
>
>Then you seem to be Continental.
>
>>Is this format understood by javascript and if so, is there
>>a javascript function to format this to "yyyy-mm-dd", that
>>is "2002-07-25"?
>
>It is, but you may not need to use that.
>
>These add a new Method to the Date Object; don't re-parse unnecessarily.
>
>function LZ(x) {return(x<0||x>9?"":"0")+x}
>
>Date.prototype.ISOlocaldateStr =
> new Function(" /* Date.ISOlocaldateStr */ with (this)\n return " +
> "getFullYear()+'-'+LZ(getMonth()+1)+'-'+LZ(getDate())")
>
>// then
>
> D = new Date("Thu Jul 25 00:00:00 UTC+0200 2002")
> D.ISOlocaldateStr() // --> 2002-07-24
> D // --> Wed Jul 24 23:00:00 UTC+0100 2002
>
>See below.

Thanks Dr,
seems smart and useful!!
And that "Thu Jul 25 00:00:00 UTC+0200 2002" turned into
'2002-07-24' I guess is only a typo? :-)
Larry

Dr John Stockton

unread,
May 9, 2003, 4:58:47 PM5/9/03
to
JRS: In article <bm1nbv8ou95gt6d3h...@4ax.com>, seen in
news:comp.lang.javascript, LarryM <lm1...@telia.com> posted at Fri, 9
May 2003 10:46:50 :-

>On Thu, 8 May 2003 20:27:42 +0100, Dr John Stockton
><sp...@merlyn.demon.co.uk> wrote:
>>JRS: In article <6h8kbvoi81m89t83o...@4ax.com>, seen in
>>news:comp.lang.javascript, LarryM <lm1...@telia.com> posted at Thu, 8
>>May 2003 09:23:52 :-
>>>
>>>I have a Calendar (web) that returns a date value
>>>"Thu Jul 25 00:00:00 UTC+0200 2002".
>>
>>Then you seem to be Continental.

>>// then


>>
>> D = new Date("Thu Jul 25 00:00:00 UTC+0200 2002")
>> D.ISOlocaldateStr() // --> 2002-07-24
>> D // --> Wed Jul 24 23:00:00 UTC+0100 2002
>>
>>See below.

>And that "Thu Jul 25 00:00:00 UTC+0200 2002" turned into


>'2002-07-24' I guess is only a typo? :-)

Indeed no; the lines are copy'n'pasted from test results.

You are using GMT+0200, in (?) Italy; so the date given is 2200h on the
24th in UTC, which is 2300 on the 24th in British Summer Time.

The same code for you should give 2002-07-25 0000h UTC+0200.

--
© John Stockton, Surrey, UK. j...@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
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 moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

LarryM

unread,
May 10, 2003, 5:34:19 AM5/10/03
to
On Fri, 9 May 2003 21:58:47 +0100, Dr John Stockton
<sp...@merlyn.demon.co.uk> wrote:

>JRS: In article <bm1nbv8ou95gt6d3h...@4ax.com>, seen in
>news:comp.lang.javascript, LarryM <lm1...@telia.com> posted at Fri, 9
>May 2003 10:46:50 :-
>>On Thu, 8 May 2003 20:27:42 +0100, Dr John Stockton
>><sp...@merlyn.demon.co.uk> wrote:
>>>JRS: In article <6h8kbvoi81m89t83o...@4ax.com>, seen in
>>>news:comp.lang.javascript, LarryM <lm1...@telia.com> posted at Thu, 8
>>>May 2003 09:23:52 :-
>>>>
>>>>I have a Calendar (web) that returns a date value
>>>>"Thu Jul 25 00:00:00 UTC+0200 2002".
>>>
>>>Then you seem to be Continental.
>
>>>// then
>>>
>>> D = new Date("Thu Jul 25 00:00:00 UTC+0200 2002")
>>> D.ISOlocaldateStr() // --> 2002-07-24
>>> D // --> Wed Jul 24 23:00:00 UTC+0100 2002
>>>
>>>See below.
>
>>And that "Thu Jul 25 00:00:00 UTC+0200 2002" turned into
>>'2002-07-24' I guess is only a typo? :-)
>
>Indeed no; the lines are copy'n'pasted from test results.
>
>You are using GMT+0200, in (?) Italy; so the date given is 2200h on the
>24th in UTC, which is 2300 on the 24th in British Summer Time.
>
>The same code for you should give 2002-07-25 0000h UTC+0200.
>

Thanks John for clearing that out!
I am glad that you pointed me to your code!
/Larry
(Sweden by the way...).

0 new messages