Play with date

M

matt

Hello,

I'm french so, excuse my english...

Here, I add one hour to a date :

var now = new Date();
var time = now.getTime();
time += (1000 * 60 * 60);
now.setTime(time);

Please, how I can do for add one month..

Thanks for you answer,

Matt...
 
T

Thomas 'PointedEars' Lahn

matt said:
Here, I add one hour to a date :

var now = new Date();
var time = now.getTime();
time += (1000 * 60 * 60);
now.setTime(time);

Better:

now.setHours(now.getHours() + 1);
Please, how I can do for add one month..

now.setMonth(now.getMonth() + 1);
Thanks for you answer,

You are welcome.


PointedEars
 
M

matt

Thomas 'PointedEars' Lahn a écrit :
Better:

now.setHours(now.getHours() + 1);


now.setMonth(now.getMonth() + 1);


You are welcome.


PointedEars

OK, thanks.

I try it tomorrow,

Matt...
 
M

matt

matt a écrit :
Thomas 'PointedEars' Lahn a écrit :

OK, thanks.

I try it tomorrow,

Matt...

me too, just one question (I don't try your method):

for example the date is 2008 12 and I add one month with your method,
the date after is 2009 01 ???

Matt...
 
R

RobG

matt a écrit :







me too, just one question (I don't try your method):

for example the date is 2008 12 and I add one month with your method,
the date after is 2009 01 ???

Yes. It also works for subraction: 2009 01 minus one month gives 2008
12. The same mechanism works for other date and time components too.

You can also do things like:

var d = new Date('2009/03/00');
document.write(d.getDate());

to find the number of days in February, 2009.
 
K

kindy

var d = new Date('2009/01/31');
d.setMonth(d.getMonth() + 1);


then,
d will be '2009/03/03'.

the problem is What is 'one month'?
 
T

Thomas 'PointedEars' Lahn

kindy said:
var d = new Date('2009/01/31');
d.setMonth(d.getMonth() + 1);

then,
d will be '2009/03/03'.

Because there is no 2009-02-31 CE. The last day of 2009-02 CE is defined to
be 2009-02-29 CE. The first day of 2009-03 CE is defined to be 2009-03-01
CE. Add the two extra days and you'll get 2009-03-03 CE.
the problem is What is 'one month'?

Exactly. ISTM, time and the calendar, as humble attempts to bring
understanding into the rather odd (in contrast to even) numbers than
humankind has observed in the rotation of its home planet, the revolution
around its primary, and the reflection and shadow created by the primary's
light on the planet's twin (for those who don't know yet: the Moon, or
Luna), along with its relation to neighboring stars, is probably the most
complex and most contradicting algorithm humankind has ever conceived.

Take only the "western" hemisphere and business world for an example: the
business year is divided into 360 days (instead of 365.26~), 52 (or 50)
weeks, 12 months, 4 weeks a month but 30 or 20 work days a month (depending
on the context), but 5 to 7 work days a week (depending on the context), 24
(or 8 to 10; instead of 23.93~) hours a day, 60 minutes an hour, 60 seconds
a minute, 1000 milliseconds a second, and so forth. And then we also have
leap years and even leap seconds (there was one shortly ago), and time
zones, and atomic time etc. pp. It is rather amazing that nobody loses
their mind when thinking about it.
[Top post]

Please don't. And trim your quotes.

<http://jibbering.com/faq/#posting>


PointedEars
 
D

Dr J R Stockton

Because there is no 2009-02-31 CE.  The last day of 2009-02 CE is defined to
be 2009-02-29 CE.

Perhaps you should re-read ISO 8601. Or buy a diary.

the
business year is divided into 360 days (instead of 365.26~),

Why .26- as opposed to .25- ?
52 (or 50) weeks,

or 53
24 (or 8 to 10; instead of 23.93~) hours a day

Why .93- as opposed to .93+ ?

It is rather amazing that nobody loses
their mind when thinking about it.

That includes a manifestly false assumption.



The OP (and others) should in all cases of time/date arithmetic
consider whether local clock time or UTC should be used, bearing in
mind that working in UTC is considerably more efficient and that it is
easy to change the value of a Date Object between representing a
certain YMDhms in UTC and the date in local time (but realising that
there is a biennial difficulty in many locations with local time).

And see <URL:http://www.merlyn.demon.co.uk/js-date1.htm#Incr>.

Tomorrow's date is perhaps best obtained with
 
T

Thomas 'PointedEars' Lahn

Dr said:
Perhaps you should re-read ISO 8601.

Ahh, yes. February has only 28 days this year, of course.
Or buy a diary.

Fair enough :)
Why .26- as opposed to .25- ?

That's a tilde (to indicate a rounded value), not a minus. According to
Wikipedia's featured and locked article[1], Terra's orbital period has been
calculated (measured?) as being approximately 365.256366 days (1.0000175
yr). I have seen this number elsewhere before.

OK, so there we have another ambiguity.
Why .93- as opposed to .93+ ?

Again, it's a tilde (ISTM you would need to use a better font). Terra's
siderial rotation period has been calculated (measured?) as being
0.99726968 d, or 23 h 56 min 4.100 s (ibid.) which approximates to 23.93~ h
(periodic 3 if we ignore the seconds).

That includes a manifestly false assumption.

Does it? Count the ambiguities in the terms that are commonly used with
time and calendar to begin with.
Tomorrow's date is perhaps best obtained with

You were saying?


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <59a311ef-09e8-43c1-bb94-b582905fb183@o4
0g2000prn.googlegroups.com>, Tue, 20 Jan 2009 14:06:45, RobG
Yes. It also works for subraction: 2009 01 minus one month gives 2008
12. The same mechanism works for other date and time components too.

ISTR finding, in at least one browser, that it does not work in all very
extreme cases. Possibly some part of the internal arithmetic is/was
implemented in 16-bit.

However, in IE7 & FF3, and recent Opera & Safari,
D = new Date() ; D.setMonth(1200000) ; D.setDate(-36524250 - 6)
does set Christmas Day last year.

For those with interests in the First Century, offsetting the year by
+100 and the month by -1200 enables use of new Date (Y, M, D) .


I have today twice been struck by a bug when posting with Google.
Articles were posted prematurely. In each case, something like
D = new Date() ; D.setHours(33)
as showing perhaps the best way to move a Date Object to the following
civil day.


Geoff : I would advise against using, for the identifier of a variable,
any "word" which is particularly common in English or in HTML. It is
perfectly legitimate; but can be intensely annoying if later there is a
need to use a machine search for one or other of those manifestations.
So, if your "div" can be replaces by, say, "divn" or "dvn" ...
 
R

RobG

On Jan 21, 10:14 pm, Thomas 'PointedEars' Lahn <[email protected]>
wrote:
[...]
Terra's
siderial rotation period has been calculated (measured?) as being
0.99726968 d, or 23 h 56 min 4.100 s (ibid.) which approximates to 23.93~h

But we aren't concerned with siderial time here.
 
T

Thomas 'PointedEars' Lahn

RobG said:
Thomas 'PointedEars' Lahn wrote:
[...]
Terra's siderial rotation period has been calculated (measured?)
as being 0.99726968 d, or 23 h 56 min 4.100 s (ibid.) which
approximates to 23.93~ h

But we aren't concerned with siderial time here.

I'm afraid you miss the point.


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>,
Wed, 21 Jan 2009 13:14:10, Thomas 'PointedEars' Lahn
Dr J R Stockton wrote:

Does it? Count the ambiguities in the terms that are commonly used with
time and calendar to begin with.

It is the "nobody loses ..." to which I refer.
 
J

John G Harris

According to
Wikipedia's featured and locked article[1], Terra's orbital period has been
calculated (measured?) as being approximately 365.256366 days (1.0000175
yr). I have seen this number elsewhere before.
<snip>

There's more than one definition of a year. The one relevant to
calendars is, from memory, 365.2422 days/year. Compare this with the
Gregorian calendar which has
(400*365 + 100 - 3)/400 = 365.2425 days/year. Pretty close.

John
 
T

Thomas 'PointedEars' Lahn

John said:
Thomas said:
According to Wikipedia's featured and locked article[1], Terra's orbital
period has been calculated (measured?) as being approximately 365.256366
days (1.0000175 yr). I have seen this number elsewhere before.

There's more than one definition of a year. [...]

My point exactly. Thanks for your attention.


PointedEars
 

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
474,116
Messages
2,570,699
Members
47,274
Latest member
SeleneHgw8

Latest Threads

Top