473,395 Members | 2,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

adding to a ddmmyyyy date

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.

Aug 8 '07 #1
17 3423
On Aug 8, 3:16 am, ginajohnst <ginajoh...@gmail.comwrote:
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.

Aug 8 '07 #2
In comp.lang.javascript message <46**************@PointedEars.de>, Wed,
8 Aug 2007 10:42:09, Thomas 'PointedEars' Lahn <Po*********@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.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 8 '07 #3
In comp.lang.javascript message <f9*********@drn.newsguy.com>, Wed, 8
Aug 2007 05:48:47, Lee <RE**************@cox.netposted:
>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.
Aug 8 '07 #4
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>
Aug 8 '07 #5
Thomas 'PointedEars' Lahn said the following on 8/8/2007 6:18 PM:
ginajohnst wrote:
>On Aug 8, 9:48 pm, Lee <REM0VElbspamt...@cox.netwrote:
>>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/
Aug 8 '07 #6
Lee
Thomas 'PointedEars' Lahn said:
>
ginajohnst wrote:
>On Aug 8, 9:48 pm, Lee <REM0VElbspamt...@cox.netwrote:
>>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
--

Aug 8 '07 #7
In comp.lang.javascript message <Xn********************@194.109.133.242>
, Wed, 8 Aug 2007 21:32:37, Evertjan. <ex**************@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.
Aug 9 '07 #8
Dr J R Stockton wrote on 09 aug 2007 in comp.lang.javascript:
In comp.lang.javascript message <Xn********************@194.109.133.242>
, Wed, 8 Aug 2007 21:32:37, Evertjan. <ex**************@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?
Because this

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

is less understandable, so is bound to have more mistakes made.
>>or, also easier:
d.setDate(d.getDate() + 50);
Pentakosta [Gr., 50] gives a perfect Pesach to Shavuot+1 arythmatic.
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.
Yes, indeed!
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 9 '07 #9
Lee
Dr J R Stockton said:
>
In comp.lang.javascript message <f9*********@drn.newsguy.com>, Wed, 8
Aug 2007 05:48:47, Lee <RE**************@cox.netposted:
>>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.
--

Aug 9 '07 #10
In comp.lang.javascript message <f9*********@drn.newsguy.com>, Wed, 8
Aug 2007 16:40:17, Lee <RE**************@cox.netposted:
>Thomas 'PointedEars' Lahn said:
>...
Can you explain how your ludicrous multiplication method works
if the resulting date is earlier than the 10th of the month?
The multiplication method is currently good for generating YYYYMMDD.

It is not good for DDMMYYYY or 8-digit FFF.

OTOH, given that separators are required, consider

d = new Date("1234/01/02") // 1234 Jan 2
X =
String(d.getFullYear()*1e4 + d.getMonth()*1e2 + d.getDate() + 100)
.replace(/(\d{4})(\d\d)(\d\d)/, "$3/$2/$1");

Change the \d{4} to \d+ and the code will be shorter and last longer.

--
(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.
Aug 9 '07 #11
In comp.lang.javascript message <fZ**************@invalid.uk.co.demon.me
rlyn.invalid>, Fri, 10 Aug 2007 21:04:47, Dr J R Stockton
<jr*@merlyn.demon.co.ukposted:
>In comp.lang.javascript message <46**************@PointedEars.de>, Fri,
10 Aug 2007 09:27:13, Thomas 'PointedEars' Lahn <Po*********@web.de>
posted:
>>Dr J R Stockton wrote:
>>Javascript lacks specific syntax for creating a Date Object of a given
Ordinal Date;

Define "Ordinal Date". If you mean something like the number of
milliseconds since the beginning of the UNIX era, then

It is defined in ISO 8601, which is IIRC released as an EN and possibly
a DIN. Alas, 8601 is AFAIK not free (unlike 8602); but the definition
is easily found by Google.
Correction : I had forgotten that ISO 8601:2004 was in fact available
free in PDF, from ISO, <http://isotc.iso.org/livelink/livelink?func=ll&o
bjId=4020763&objAction=browse&sort=nameprobably; and from OMG -- links
are on my site, but I forget which I actually used. It seems that ISO
do allow one to pay for the paper version and for the PDF, if one so
wishes.

--
(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.
Aug 15 '07 #12

On Aug 8, 12:16 am, ginajohnst <ginajoh...@gmail.comwrote:
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.
I'll try a more explicit approach.

The first step is to add 50 days to the date object.

// get the date.
var date = new Date();
var dayOfMonth = date.getDate()); // This API method "getDate" should
be renamed to "getDayOfMonth".

// add 50 days.
date.setDate( dayOfMonth + 50 );
Any help would be appreciated.
Write a unit test to verify each step.

Here is a horrible ad hoc test:
javascript:alert(new Date (new Date().setDate( new Date().getDate() +
50 ) - ( 50 * 24 * 60 * 60 * 1000 ) ) )

Garrett

Aug 23 '07 #13
The problem appears that there is no standard way of formatting a Date
to a customized string.

It seems possible to format a Date string using a RegExp, but the Date
string cannot be guaranteed to be in a specific format.

I would like to see this added to ES4.

var s = "EEE, MMM d, yyyy"
var df = DateFormat( s );
var formattedDay df.format( new Date() ); // Thu, Aug 23, 2007.

http://java.sun.com/j2se/1.5.0/docs/...ateFormat.html
Aug 23 '07 #14
dh**********@gmail.com wrote:
[...]
date.setDate( dayOfMonth + 50 );
You are a *little* late. <Xn********************@194.109.133.242>
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Aug 24 '07 #15
In comp.lang.javascript message <11*********************@z24g2000prh.goo
glegroups.com>, Thu, 23 Aug 2007 23:48:11, "dh**********@gmail.com"
<dh**********@gmail.composted:
>The problem appears that there is no standard way of formatting a Date
to a customized string.

It seems possible to format a Date string using a RegExp, but the Date
string cannot be guaranteed to be in a specific format.

I would like to see this added to ES4.

var s = "EEE, MMM d, yyyy"
var df = DateFormat( s );
var formattedDay df.format( new Date() ); // Thu, Aug 23, 2007.

http://java.sun.com/j2se/1.5.0/docs/...ateFormat.html
Since that is Java, and this is a Javascript newsgroup, ISTM that you
have a severe sagacity deficiency.
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.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 24 '07 #16
In comp.lang.javascript message <11*********************@r23g2000prd.goo
glegroups.com>, Thu, 23 Aug 2007 23:43:35, "dh**********@gmail.com"
<dh**********@gmail.composted:
>Here is a horrible ad hoc test:
javascript:alert(new Date (new Date().setDate( new Date().getDate() +
50 ) - ( 50 * 24 * 60 * 60 * 1000 ) ) )
That will, at present, show the present date and time.

But change both instances of 50 to 130, and it will probably show you an
hour after present time, but might show the present time or an hour (or
half an hour, but that's unlikely) before present time.

Until you understand that, it would be better if you did not try to
answer Javascript date questions.

--
(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.
Aug 24 '07 #17
Dr J R Stockton said the following on 8/24/2007 4:24 PM:
In comp.lang.javascript message <11*********************@z24g2000prh.goo
glegroups.com>, Thu, 23 Aug 2007 23:48:11, "dh**********@gmail.com"
<dh**********@gmail.composted:
>The problem appears that there is no standard way of formatting a Date
to a customized string.

It seems possible to format a Date string using a RegExp, but the Date
string cannot be guaranteed to be in a specific format.

I would like to see this added to ES4.

var s = "EEE, MMM d, yyyy"
var df = DateFormat( s );
var formattedDay df.format( new Date() ); // Thu, Aug 23, 2007.

http://java.sun.com/j2se/1.5.0/docs/...ateFormat.html

Since that is Java, and this is a Javascript newsgroup, ISTM that you
have a severe sagacity deficiency.
If you had bothered to read, and comprehend, the line preceding the code
posted you would have realized some things that you are either too
arrogant or too ignorant to admit, possibly - and probably - both.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 25 '07 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dave Mann | last post by:
Hi to all those Knowledge Brokers out there! I am trying to get a field to return a date (Next Due date) only if a box is checked (is it planned or unplanned)and Type of Work performed (i.e if...
1
by: John Feeley | last post by:
am tring to add a number of years to a dob. im doing this by adding my date+years*365.26 I get a string of numbers. I then convert the number in the next column to actual date again. I'm getting...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
2
by: markryde | last post by:
Hello, I am trying to add the current date to a file name in python script like thus: import os import sys import rpm import time import datetime
6
by: ninrulz | last post by:
I have created a database that has over 70 queries and over 40k records. I used the OutputTo action to export the queries to Excel. I would like the functionality of adding the date (Month-YY) to...
6
by: Luvin lunch | last post by:
Hi, I'm new to access and am very wary of dates as I have limited experience in their manipulation and I know if they're not done properly things can turn ugly quickly. I would like to use a...
27
by: Rio Liaden | last post by:
Hi all! My database allows all employees to enter their time and get a report at any time. The first screen has a calendar which is locked to the last day of the pay period and that date is the...
3
by: seepnezorf | last post by:
I keep a database of projects. I have a memo field in which I keep dated notes about the status. Above the memo field is a button called "update date." When I click on the button it adds into the...
3
by: janetopps | last post by:
I have a news website, with asp pages, which was on Access, and i upgraded to MySQL, i used Bullzip to transfer the data. It had about 1000 pages, which im now able to pull up on the public side. Im...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.