SearchSearch   

adding to a ddmmyyyy date

 
Goto page 1, 2
   Webmaster Forums (Home) -> Javascript RSS
Next:  Dynamically Generated table does not apply styles  
Author Message
ginajohnst

External


Since: Aug 08, 2007
Posts: 3



(Msg. 1) Posted: Wed Aug 08, 2007 12:16 am
Post subject: adding to a ddmmyyyy date
Archived from groups: comp>lang>javascript (more info?)

Hi All.

I'm having a problem adding days to a date.

My date is in the string format dd/mm/yyyy eg. 23/08/2007 in my
form field.

I can't work out how to add 50 days to that date and then write it to
another form field.

Any help would be appreciated.
Back to top
David Mark

External


Since: Aug 06, 2007
Posts: 358



(Msg. 2) Posted: Wed Aug 08, 2007 12:19 am
Post subject: Re: adding to a ddmmyyyy date
Archived from groups: per prev. post (more info?)

On Aug 8, 3:16 am, ginajohnst <ginajoh... DeleteThis @gmail.com> wrote:
> Hi All.
>
> I'm having a problem adding days to a date.
>
> My date is in the string format dd/mm/yyyy eg. 23/08/2007 in my
> form field.
>
> I can't work out how to add 50 days to that date and then write it to
> another form field.
>
> Any help would be appreciated.

Which part can't you figure out? See the FAQ for date manipulation.
To change a form element's value, set its value property.
Back to top
ginajohnst

External


Since: Aug 08, 2007
Posts: 3



(Msg. 3) Posted: Wed Aug 08, 2007 2:20 am
Post subject: Re: adding to a ddmmyyyy date
Archived from groups: per prev. post (more info?)

On Aug 8, 5:42 pm, Thomas 'PointedEars' Lahn <PointedE....DeleteThis@web.de>
wrote:
> ginajohnst wrote:
> > I'm having a problem adding days to a date.
>
> > My date is in the string format dd/mm/yyyy eg. 23/08/2007 in my
> > form field.
>
> > I can't work out how to add 50 days to that date and then write it to
> > another form field.
>
> Quickhack:
>
> var
> a = formField.value.split("/"),
> d = new Date(
> parseInt(a[2], 10), parseInt(a[1], 10), parseInt(a[0], 10) + 50);
>
> anotherFormField.value =
> String(d.getDate() * 1e6 + d.getMonth() * 1e4 + d.getFullYear())
> .replace(/(\d\d)(\d\d)(\d{4})/, "$1/$2/$3");
>
> However, you better separate the components, one `input' or `select' element
> for each one, in order to make your date input unambiguous.

Hi Thomas.

Thanks for that. I'll make sure that I understand it completely
before I use your example.

Thanks again.
Back to top
Lee

External


Since: Jun 09, 2004
Posts: 1556



(Msg. 4) Posted: Wed Aug 08, 2007 5:48 am
Post subject: Re: adding to a ddmmyyyy date
Archived from groups: per prev. post (more info?)

ginajohnst said:
>
>On Aug 8, 5:42 pm, Thomas 'PointedEars' Lahn <PointedE... RemoveThis @web.de>
>wrote:
>> ginajohnst wrote:
>> > I'm having a problem adding days to a date.
>>
>> > My date is in the string format dd/mm/yyyy eg. 23/08/2007 in my
>> > form field.
>>
>> > I can't work out how to add 50 days to that date and then write it to
>> > another form field.
>>
>> Quickhack:
>>
>> var
>> a = formField.value.split("/"),
>> d = new Date(
>> parseInt(a[2], 10), parseInt(a[1], 10), parseInt(a[0], 10) + 50);
>>
>> anotherFormField.value =
>> String(d.getDate() * 1e6 + d.getMonth() * 1e4 + d.getFullYear())
>> .replace(/(\d\d)(\d\d)(\d{4})/, "$1/$2/$3");
>>
>> However, you better separate the components, one `input' or `select' element
>> for each one, in order to make your date input unambiguous.
>
>Hi Thomas.
>
>Thanks for that. I'll make sure that I understand it completely
>before I use your example.

Once you understand it completely, you should be able to come up
with a much simpler solution that actually works.

http://docs.sun.com/source/816-6408-10/date.htm


--
Back to top
Thomas 'PointedEars' Lahn

External


Since: Sep 05, 2004
Posts: 3405



(Msg. 5) Posted: Wed Aug 08, 2007 10:42 am
Post subject: Re: adding to a ddmmyyyy date
Archived from groups: per prev. post (more info?)

ginajohnst wrote:
> I'm having a problem adding days to a date.
>
> My date is in the string format dd/mm/yyyy eg. 23/08/2007 in my
> form field.
>
> I can't work out how to add 50 days to that date and then write it to
> another form field.

Quickhack:

var
a = formField.value.split("/"),
d = new Date(
parseInt(a[2], 10), parseInt(a[1], 10), parseInt(a[0], 10) + 50);

anotherFormField.value =
String(d.getDate() * 1e6 + d.getMonth() * 1e4 + d.getFullYear())
.replace(/(\d\d)(\d\d)(\d{4})/, "$1/$2/$3");

However, you better separate the components, one `input' or `select' element
for each one, in order to make your date input unambiguous.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Back to top
ginajohnst

External


Since: Aug 08, 2007
Posts: 3



(Msg. 6) Posted: Wed Aug 08, 2007 2:05 pm
Post subject: Re: adding to a ddmmyyyy date
Archived from groups: per prev. post (more info?)

On Aug 8, 9:48 pm, Lee <REM0VElbspamt... RemoveThis @cox.net> wrote:
> ginajohnst said:
>

> Once you understand it completely, you should be able to come up
> with a much simpler solution that actually works.
>
> http://docs.sun.com/source/816-6408-10/date.htm

Yep. I've ended up with this so far and I think a bit of testing will
give me the result I want.

var a = document.getElementById('date1').value.split("/");

d = new Date((a[2]), (a[1])-1, (a[0]));
d = new Date(d.getTime() + 50*24*60*60*1000);

document.getElementById('date2').value = String(d.getDate() + '/' +
(d.getMonth()+1) + '/' + d.getFullYear());
Back to top
Dr J R Stockton

External


Since: May 20, 2007
Posts: 386



(Msg. 7) Posted: Wed Aug 08, 2007 7:58 pm
Post subject: Re: adding to a ddmmyyyy date
Archived from groups: per prev. post (more info?)

In comp.lang.javascript message <46B981E1.8060806 RemoveThis @PointedEars.de>, Wed,
8 Aug 2007 10:42:09, Thomas 'PointedEars' Lahn <PointedEars RemoveThis @web.de>
posted:

>Quickhack:

but not well tested. It gives
23/02/2007 + 50 -> 12/04/2007
23/08/2007 + 50 -> 12/10/2007

But February is shorter than August, and should give days 3 more.

> var
> a = formField.value.split("/"),
> d = new Date(
> parseInt(a[2], 10), parseInt(a[1], 10), parseInt(a[0], 10) + 50);

d = new Date(a[2], a[1]-1, a[0]+50) // suffices

One can replace, in this context, parseInt by unary +; and that will in
context be effectively implicit.

> anotherFormField.value =
> String(d.getDate() * 1e6 + d.getMonth() * 1e4 + d.getFullYear())
> .replace(/(\d\d)(\d\d)(\d{4})/, "$1/$2/$3");

Do you find that better than, with suitable LZ,

anotherFormField.value =
LZ(d.getDate()) + '/' + LZ(d.getMonth()+1) + '/' + d.getFullYear()

// ??

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Back to top
Dr J R Stockton

External


Since: May 20, 2007
Posts: 386



(Msg. 8) Posted: Wed Aug 08, 2007 7:58 pm
Post subject: Re: adding to a ddmmyyyy date
Archived from groups: per prev. post (more info?)

In comp.lang.javascript message <f9ce3f02g65.TakeThisOut@drn.newsguy.com>, Wed, 8
Aug 2007 05:48:47, Lee <REM0VElbspamtrap.TakeThisOut@cox.net> posted:

>>> > My date is in the string format dd/mm/yyyy eg. 23/08/2007 in my

>Once you understand it completely, you should be able to come up
>with a much simpler solution that actually works.
>
>http://docs.sun.com/source/816-6408-10/date.htm

/* There must be a nicer-looking version of that page, with a margin */
/* Note its error in a line starting like <tt>milliseconds</tt> */
/* They seem to ignore that UT != UTC */

Which parts of that page d0 you believe to deal with the reading and
writing of non-FFF numeric date strings??

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
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.
Back to top
Evertjan.

External


Since: Sep 30, 2005
Posts: 2391



(Msg. 9) Posted: Wed Aug 08, 2007 9:32 pm
Post subject: Re: adding to a ddmmyyyy date
Archived from groups: per prev. post (more info?)

ginajohnst wrote on 08 aug 2007 in comp.lang.javascript:

> Yep. I've ended up with this so far and I think a bit of testing will
> give me the result I want.
>
> var a = document.getElementById('date1').value.split("/");
>
> d = new Date((a[2]), (a[1])-1, (a[0]));

too much ()()()

var d

> d = new Date(d.getTime() + 50*24*60*60*1000);

safer:
d.setTime(d.getTime() + 50*24*60*60*1000);
or, also easier:
d.setDate(d.getDate() + 50);

> document.getElementById('date2').value = String(d.getDate() + '/' +
> (d.getMonth()+1) + '/' + d.getFullYear());

No need for String(), the conversion is automatic.

I would add an extra zero on the single figure dates and months.

Try [leaving the DOM part for you to insert]:

==========================================
<script type='text/javascript'>

var a = '22/11/2006'.split("/");

var d = new Date(a[2], a[1]-1, a[0]);
d.setDate(d.getDate() + 50);

var r =
T(d.getDate())+'/'+T(d.getMonth()+1)+'/'+d.getFullYear();

alert(r);

function T(x){
return (x>9)?xSad'0'+x);
};

</script>
===========================================

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Back to top
Thomas 'PointedEars' Lahn

External


Since: Sep 05, 2004
Posts: 3405



(Msg. 10) Posted: Thu Aug 09, 2007 12:18 am
Post subject: Re: adding to a ddmmyyyy date
Archived from groups: per prev. post (more info?)

ginajohnst wrote:
> On Aug 8, 9:48 pm, Lee <REM0VElbspamt....TakeThisOut@cox.net> wrote:
>> ginajohnst said:
>
>> Once you understand it completely, you should be able to come up
>> with a much simpler solution that actually works.

My solution works alright. If one had tested it, as I did, one would have
known. But what to expect from a noname person with headers disregarding
Internet standards, Netiquette and their ISP's terms of service?

>> http://docs.sun.com/source/816-6408-10/date.htm
>
> Yep. I've ended up with this so far and I think a bit of testing will
> give me the result I want.
>
> var a = document.getElementById('date1').value.split("/");

Your continued unnecessary mixing of DOM 2 features with features
originating from DOM 0 is giving me headaches. As it should give you.

> d = new Date((a[2]), (a[1])-1, (a[0]));

`08' may be parsed as an invalid octal value, hence parseInt(, 10);
Without parseInt(), the parens add no more than further confusion.

> d = new Date(d.getTime() + 50*24*60*60*1000);

Won't work as expected. I'm sure John will happily point out why.

I can't think of a valid reason why

d = new Date(..., ..., parseInt(a[0], 10) + 50);

shouldn't be used.

> document.getElementById('date2').value = String(d.getDate() + '/' +
> (d.getMonth()+1) + '/' + d.getFullYear());

String conversion is unnecessary with that error-prone an approach.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Back to top
Randy Webb

External


Since: Aug 24, 2004
Posts: 4981



(Msg. 11) Posted: Thu Aug 09, 2007 12:18 am
Post subject: Re: adding to a ddmmyyyy date
Archived from groups: per prev. post (more info?)

Thomas 'PointedEars' Lahn said the following on 8/8/2007 6:18 PM:
> ginajohnst wrote:
>> On Aug 8, 9:48 pm, Lee <REM0VElbspamt... DeleteThis @cox.net> wrote:
>>> ginajohnst said:
>>> Once you understand it completely, you should be able to come up
>>> with a much simpler solution that actually works.
>
> My solution works alright.

Is that why you agreed with John when he pointed out a flaw in it?

> If one had tested it, as I did, one would have known.

Obviously you didn't or you wouldn't have agreed with John in his
assessment of it having flaws in it.

> But what to expect from a noname person with headers disregarding
> Internet standards, Netiquette and their ISP's terms of service?

Whatever you were smoking, snorting or swallowing when you wrote that
garbage you should endeavor to stay away from.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Back to top
Lee

External


Since: Jun 09, 2004
Posts: 1556



(Msg. 12) Posted: Thu Aug 09, 2007 12:18 am
Post subject: Re: adding to a ddmmyyyy date
Archived from groups: per prev. post (more info?)

Thomas 'PointedEars' Lahn said:
>
>ginajohnst wrote:
>> On Aug 8, 9:48 pm, Lee <REM0VElbspamt....TakeThisOut@cox.net> wrote:
>>> ginajohnst said:
>>
>>> Once you understand it completely, you should be able to come up
>>> with a much simpler solution that actually works.
>
>My solution works alright. If one had tested it, as I did, one would have
>known. But what to expect from a noname person with headers disregarding
>Internet standards, Netiquette and their ISP's terms of service?

You think you tested that?

Can you explain how it can work properly when you don't subtract
one from the month before passing it to the Date constructor?

Can you explain how your ludicrous multiplication method works
if the resulting date is earlier than the 10th of the month?

Try these inputs, genius:
01/11/2008
13/11/2008


--
Back to top
Thomas 'PointedEars' Lahn

External


Since: Sep 05, 2004
Posts: 3405



(Msg. 13) Posted: Thu Aug 09, 2007 12:40 am
Post subject: Re: adding to a ddmmyyyy date
Archived from groups: per prev. post (more info?)

Dr J R Stockton wrote:
> [...] Thomas 'PointedEars' Lahn [...] posted:
>> Quickhack:
>
> but not well tested.

Hence *quick**hack*.

> It gives
> 23/02/2007 + 50 -> 12/04/2007
> 23/08/2007 + 50 -> 12/10/2007
>
> But February is shorter than August, and should give days 3 more.

Confirmed in FF 2.0.0.6 :-/ But where is the error here? If it isn't mine,
can someone quickly point out anything in ES3 that specifies it?

>> var
>> a = formField.value.split("/"),
>> d = new Date(
>> parseInt(a[2], 10), parseInt(a[1], 10), parseInt(a[0], 10) + 50);
>
> d = new Date(a[2], a[1]-1, a[0]+50) // suffices

Would that not have the same February problem as my solution?

> One can replace, in this context, parseInt by unary +; and that will in
> context be effectively implicit.

I had thought about that, but what about octals?

>> anotherFormField.value =
>> String(d.getDate() * 1e6 + d.getMonth() * 1e4 + d.getFullYear())
>> .replace(/(\d\d)(\d\d)(\d{4})/, "$1/$2/$3");
>
> Do you find that better than, with suitable LZ,
>
> anotherFormField.value =
> LZ(d.getDate()) + '/' + LZ(d.getMonth()+1) + '/' + d.getFullYear()
>
> // ??

Hm. No. I just liked your previous solution and had my quickhack based on it.


PointedEars
--
"Use any version of Microsoft Frontpage to create your site. (This won't
prevent people from viewing your source, but no one will want to steal it.)"
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Back to top
Lee

External


Since: Jun 09, 2004
Posts: 1556



(Msg. 14) Posted: Thu Aug 09, 2007 11:59 am
Post subject: Re: adding to a ddmmyyyy date
Archived from groups: per prev. post (more info?)

Dr J R Stockton said:
>
>In comp.lang.javascript message <f9ce3f02g65 RemoveThis @drn.newsguy.com>, Wed, 8
>Aug 2007 05:48:47, Lee <REM0VElbspamtrap RemoveThis @cox.net> posted:
>
>>>> > My date is in the string format dd/mm/yyyy eg. 23/08/2007 in my
>
>>Once you understand it completely, you should be able to come up
>>with a much simpler solution that actually works.
>>
>>http://docs.sun.com/source/816-6408-10/date.htm
>
>/* There must be a nicer-looking version of that page, with a margin */
>/* Note its error in a line starting like <tt>milliseconds</tt> */
>/* They seem to ignore that UT != UTC */
>
>Which parts of that page d0 you believe to deal with the reading and
>writing of non-FFF numeric date strings??

The information about the constructor and the various setters
and getters seemed useful. The question involved more than
simply reading and writing date strings.


--
Back to top
Dr J R Stockton

External


Since: May 20, 2007
Posts: 386



(Msg. 15) Posted: Thu Aug 09, 2007 3:14 pm
Post subject: Re: adding to a ddmmyyyy date
Archived from groups: per prev. post (more info?)

In comp.lang.javascript message <Xns9986EF7F65D6Deejj99.TakeThisOut@194.109.133.242>
, Wed, 8 Aug 2007 21:32:37, Evertjan. <exjxw.hannivoort.TakeThisOut@interxnl.net>
posted:
>
>> d = new Date(d.getTime() + 50*24*60*60*1000);
>
>safer:
>d.setTime(d.getTime() + 50*24*60*60*1000);

Better, yes; but why safer?

>or, also easier:
>d.setDate(d.getDate() + 50);

That's also safer. Starting at Saturday 2007-03-24 23:30, the first
gives us a Monday and the second a Sunday. 50 days are 7 weeks plus ONE
day.



Re another article : parseInt(S) for S being "08" or "09" gives zero
(the standard reluctantly allows that); so, in this context, if parseInt
is used then it does need ', 10'.

But without parseInt, "08" and "09" are taken as eight and nine;
although "077" is sixty-three, "078" is seventy-eight - at least,
wherever I've tried it. That seems compatible with ISO/IEC 16262 B.1.

Javascript lacks specific syntax for creating a Date Object of a given
Ordinal Date; it does not need any, since new Date(YYYY, 0, DDD)
suffices. For DDD, parseInt(DDD, 10) seems essential.

new Date(2007, 0, 077) // Mar 4
new Date(2007, 0, 078) // Mar 19

Similar considerations apply to YYYY - parseInt is necessary unless most
years before 800 will definitely not be used.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
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.
Back to top
Display posts from previous:   
       Webmaster Forums (Home) -> Javascript
Goto page 1, 2
Page 1 of 2

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum