Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #16270

Re: How to construct a tomorrow?

Message-ID <3208870.M5aPXhl1hL@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2012-09-30 14:29 +0200
Subject Re: How to construct a tomorrow?
Newsgroups comp.lang.javascript
References <6aae09ab-0d45-4c30-accf-909d7445c6a2@o8g2000yqm.googlegroups.com> <k401aj$jnh$1@speranza.aioe.org> <ecUo+7MM7dZQFwAQ@invalid.uk.co.demon.merlyn.invalid> <k45dmn$8j7$1@speranza.aioe.org>
Followup-To comp.lang.javascript

Followups directed to: comp.lang.javascript

Show all headers | View raw


Christoph Becker wrote:

> Dr J R Stockton wrote:
>> […] Christoph Becker <cmbecker69@gmx.de> posted:
>>> justaguy wrote:
>>>> how do we get tomorrow based on today?
>>> 
>>> So a simple solution is:
>>       xxxxxx naive
>>>
>>> var today = new Date();
>>> var tomorrow = new Date(today.getTime() + 86400000)
>> 
>> And what happens when the interval crosses 01:00 UTC on the last Sundays
>> of March and October? (dates and times are different for chronologically
>> inconvenient locations).
> 
> I *thought* a day is defined as having 86,400 seconds (cf.
> <http://www.bipm.org/en/si/si_brochure/chapter4/table6.html>).  If I am
> mistaken in this view, please correct me -- I'll appreciate an
> explanation or a link to one.

There are more definitions of "day" than that.  You can start by reading the 
corresponding Wikipedia article.

Even in standard time a day can have more or less seconds.  For example, UTC 
includes the concept of the leap second.  (The Date object does not include 
that concept, though.)
 
> If you were referring to summer time issues,

He was.

> AFAIK this wouldn't be relevant here, as the calculation is "internal",

It would be relevant.

> and the results /should/ be faithfully presented in human readable form by
> calling toLocalDateString().

The method is named `toLocal*e*DateString'; it refers to the system 
*locale*, _not_ necessarily local time.  It is specified in ECMAScript 
Ed. 3, and fully supported since JScript 5.5.6330, JavaScript 1.5, V8 
3.7.12.12, JSC 531.22.7, Opera 7.02, and KJS 4.6.5.  Insofar it can be 
considered safe for use.

Your assumption can be easily tested (in "Mozilla/5.0 (X11; Linux i686) 
AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1"):

| > var today = new Date(2012, 9, 28);
|   var tomorrow = new Date(today.getTime() + 864e5);
|   console.log(today + "\n" + tomorrow);
| Sun Oct 28 2012 00:00:00 GMT+0200 (CEST)
| Sun Oct 28 2012 23:00:00 GMT+0100 (CET)
| 
| > console.log(today.toLocaleDateString() + "\n"
|     + tomorrow.toLocaleDateString());
| Sunday, October 28, 2012
| Sunday, October 28, 2012

So this approach evidently does not work.  But the question was how to 
construct a Date instance holding (a date of) tomorrow instead:

| > var tomorrow = new Date(2012, 9, 28);
| undefined
| 
| > tomorrow
| Sun Oct 28 2012 00:00:00 GMT+0200 (CEST)
| 
| > tomorrow.setDate(tomorrow.getDate() + 1);
| 1351465200000
| 
| > tomorrow
| Mon Oct 29 2012 00:00:00 GMT+0100 (CET)

>> Also, 86400000 is better written as 864e5 - shorter, and avoids
>> miscounting zeroes.
> 
> One might argue, that it's even better to write it as
> 
> 24 * 60 * 60 * 1000
> 
> what is not as efficient and short, but indicates the meaning even more
> clearly.

And it also shows why that number fails to accomplish "[getting] tomorrow 
based on today".


PointedEars
-- 
Sometimes, what you learn is wrong. If those wrong ideas are close to the 
root of the knowledge tree you build on a particular subject, pruning the 
bad branches can sometimes cause the whole tree to collapse.
  -- Mike Duffy in cljs, <news:Xns9FB6521286DB8invalidcom@94.75.214.39>

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How to construct a tomorrow? justaguy <lichunshen84@gmail.com> - 2012-09-26 14:47 -0700
  Re: How to construct a tomorrow? Christoph Becker <cmbecker69@gmx.de> - 2012-09-27 00:58 +0200
    Re: How to construct a tomorrow? justaguy <lichunshen84@gmail.com> - 2012-09-26 17:47 -0700
      Re: How to construct a tomorrow? Gene Wirchenko <genew@ocis.net> - 2012-09-26 19:53 -0700
        Re: How to construct a tomorrow? Dr J R Stockton <reply1239@merlyn.demon.co.uk.invalid> - 2012-09-28 18:40 +0100
          Re: How to construct a tomorrow? Gene Wirchenko <genew@ocis.net> - 2012-09-29 20:52 -0700
    Re: How to construct a tomorrow? Dr J R Stockton <reply1239@merlyn.demon.co.uk.invalid> - 2012-09-28 18:30 +0100
      Re: How to construct a tomorrow? Christoph Becker <cmbecker69@gmx.de> - 2012-09-29 02:00 +0200
        Re: How to construct a tomorrow? John G Harris <john@nospam.demon.co.uk> - 2012-09-29 10:02 +0100
        Re: How to construct a tomorrow? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-30 14:29 +0200
          Re: How to construct a tomorrow? Christoph Becker <cmbecker69@gmx.de> - 2012-09-30 23:24 +0200
        Re: How to construct a tomorrow? Dr J R Stockton <reply1239@merlyn.demon.co.uk.invalid> - 2012-09-30 19:55 +0100
  Re: How to construct a tomorrow? Dr J R Stockton <reply1239@merlyn.demon.co.uk.invalid> - 2012-09-27 21:32 +0100
    Re: How to construct a tomorrow? Christoph Becker <cmbecker69@gmx.de> - 2012-09-28 01:03 +0200
      Re: How to construct a tomorrow? Gene Wirchenko <genew@ocis.net> - 2012-09-27 16:36 -0700
      Re: How to construct a tomorrow? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-28 10:37 +0200
      Re: How to construct a tomorrow? Dr J R Stockton <reply1239@merlyn.demon.co.uk.invalid> - 2012-09-29 20:39 +0100
        Re: How to construct a tomorrow? Christoph Becker <cmbecker69@gmx.de> - 2012-09-30 23:37 +0200
    Re: How to construct a tomorrow? Andrew Poulos <ap_prog@hotmail.com> - 2012-09-28 09:25 +1000
    Re: How to construct a tomorrow? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-09-28 08:37 -0700
      Re: How to construct a tomorrow? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-28 18:48 +0200
      Re: How to construct a tomorrow? Dr J R Stockton <reply1239@merlyn.demon.co.uk.invalid> - 2012-09-29 20:40 +0100
        Re: How to construct a tomorrow? Andrew Poulos <ap_prog@hotmail.com> - 2012-09-30 18:32 +1000
    Re: How to construct a tomorrow? Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2012-09-28 23:57 +0200
      Re: How to construct a tomorrow? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-30 13:49 +0200

csiph-web