adding to a ddmmyyyy date

D

dhtmlkitchen

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
 
D

dhtmlkitchen

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/api/java/text/SimpleDateFormat.html
 
T

Thomas 'PointedEars' Lahn

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

I would like to see this added to ES4.

While you wait for that, you might as well implement it. It is not
that difficult.
var s = "EEE, MMM d, yyyy"
var df = DateFormat( s );
var formattedDay df.format( new Date() ); // Thu, Aug 23, 2007.
^
= ----------------'

So?


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
glegroups.com>, Thu, 23 Aug 2007 23:48:11, "(e-mail address removed)"
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/api/java/text/SimpleDateFormat.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.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
glegroups.com>, Thu, 23 Aug 2007 23:43:35, "(e-mail address removed)"
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.
 
D

dhtmlkitchen

While you wait for that, you might as well implement it. It is not
that difficult.

Yeah, probably less than 2 work days to write and test. toString on a
Date doesn't guarantee any format. use toUTCString() does.
From the outset, it doesn't appear that DateFormat would require
additional classes; just RegExp, Date, String. It could probably stand
on its own, cohesively, and might be less than 20k.

How common is the need for a Date formatter?

What would be the performance impact?

What would be the risk?

What are the alternatives?
^
= ----------------'


So?
It's a useful implementation with detailed documentation.

Date formatting on the server using SimpleDateFormat is reliable,
existing code. Sending a formatted date is can sometimes fulfill the
need.

However, other use cases make server side options impractical.

Personally, I would probably choose to format on the server as much as
I can, and when I can't, I would probably try the simplest thing that
could possibly work (not writing a full-featured DateFormatter unless
it were necessary).
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
oglegroups.com>, Sun, 26 Aug 2007 05:33:41, "(e-mail address removed)"
Personally, I would probably choose to format on the server as much as
I can, and when I can't, I would probably try the simplest thing that
could possibly work (not writing a full-featured DateFormatter unless
it were necessary).

Yes. Only under weird circumstances does it seem likely to be necessary
to inflict a completely versatile date formatter on everyone who
downloads a Web page.

Nowadays, I believe the default conversion of Date Object to String
gives unambiguous English. Conversion to Foreign takes a little more.

A few lines suffice to generate, for any reasonably-current date, the
ISO 8601 format YYYY-MM-DD which is understandable without ambiguity
everywhere that the Gregorian Calendar is used (USG agencies excepted).

Another to use the TLA for the month (e.g. Feb), and another one for the
TLA for the DoW (e.g. Thu).

The following, not necessarily optimum, sets S to three date forms :-

function LZ(x) { return (x<10?"0":"") + x }
M = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split(" ")
W = "Sun Mon Tue Wed Thu Fri Sat".split(" ")
D = new Date()
S = D.getFullYear()+'-'+LZ(D.getMonth()+1)+'-'+LZ(D.getDate())
S = D.getFullYear()+'-'+M[D.getMonth()]+'-'+LZ(D.getDate())
S += ", " + W[D.getDay()]
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top