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


Groups > comp.lang.javascript > #19420 > unrolled thread

About format to always use 2 digits for hour

Started by"Tony" <tony.johansson@inport.com>
First post2013-04-23 15:07 +0200
Last post2013-04-30 22:14 +0100
Articles 14 — 8 participants

Back to article view | Back to comp.lang.javascript


Contents

  About format to always use 2 digits for hour "Tony" <tony.johansson@inport.com> - 2013-04-23 15:07 +0200
    Re: About format to always use 2 digits for hour JJ <duh@nah.meh> - 2013-04-23 20:55 +0700
    Re: About format to always use 2 digits for hour "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2013-04-23 16:58 +0300
    Re: About format to always use 2 digits for hour "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-04-23 17:44 +0200
      Re: About format to always use 2 digits for hour "Mel Smith" <med_cutout_syntel@aol.com> - 2013-04-23 12:40 -0600
        Re: About format to always use 2 digits for hour Christoph Becker <cmbecker69@gmx.de> - 2013-04-23 21:02 +0200
      Re: About format to always use 2 digits for hour Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-04-24 22:12 +0100
        Re: About format to always use 2 digits for hour "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-04-25 09:39 +0200
          Re: About format to always use 2 digits for hour Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-04-26 20:56 +0100
            Re: About format to always use 2 digits for hour "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-04-27 10:50 +0200
              Re: About format to always use 2 digits for hour Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-04-28 16:49 +0100
                Re: About format to always use 2 digits for hour Stefan Weiss <krewecherl@gmail.com> - 2013-04-29 01:19 +0200
                Re: About format to always use 2 digits for hour "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-04-29 09:05 +0200
                  Re: About format to always use 2 digits for hour Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-04-30 22:14 +0100

#19420 — About format to always use 2 digits for hour

From"Tony" <tony.johansson@inport.com>
Date2013-04-23 15:07 +0200
SubjectAbout format to always use 2 digits for hour
Message-ID<kl60sb$9nn$1@dont-email.me>
Do I really have to manually use this kind of construction to always use two 
digits for hour

currentTime.setMinutes(currentTime.getMinutes() + 30);
hours = hours < 10 ? "0" + hours : hours;

//Tony 

[toc] | [next] | [standalone]


#19422

FromJJ <duh@nah.meh>
Date2013-04-23 20:55 +0700
Message-ID<10rzpyftf52m6.1exlgfkwtmdtk.dlg@40tude.net>
In reply to#19420
On Tue, 23 Apr 2013 15:07:16 +0200, Tony wrote:
> Do I really have to manually use this kind of construction to always use two 
> digits for hour
> 
> currentTime.setMinutes(currentTime.getMinutes() + 30);
> hours = hours < 10 ? "0" + hours : hours;

You can string-extract the hour part of an ISO date.
It's simpler, but slower.

[toc] | [prev] | [next] | [standalone]


#19424

From"Jukka K. Korpela" <jkorpela@cs.tut.fi>
Date2013-04-23 16:58 +0300
Message-ID<kl63t8$pgo$1@dont-email.me>
In reply to#19420
2013-04-23 16:07, Tony wrote:

> Do I really have to manually use this kind of construction to always use
> two digits for hour

No, you can use a library routine. Although this might seem to be 
overkill for such a simple operation, it really isn't. Done properly, it 
will make the code easier to manage and also make your software 
localization-ready, provided that you use a library with localization 
features, such as Globalize.js. (Not all languages and cultures use the 
"hh:mm" notation. Some use "hh.mm" for example.)

-- 
Yucca, http://www.cs.tut.fi/~jkorpela/

[toc] | [prev] | [next] | [standalone]


#19426

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2013-04-23 17:44 +0200
Message-ID<XnsA1ABB47255529eejj99@194.109.133.133>
In reply to#19420
Tony wrote on 23 apr 2013 in comp.lang.javascript:

> Do I really have to manually use this kind of construction to always
> use two digits for hour
> 
> currentTime.setMinutes(currentTime.getMinutes() + 30);
> hours = hours < 10 ? "0" + hours : hours;

Make your own function:

function two(x){
  return ('0' + x).substr(-2);
};

now = new Date();
myTime = two(now.getHours()) + ':' + two(now.getMinutes());
alert(myTime);


-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#19427

From"Mel Smith" <med_cutout_syntel@aol.com>
Date2013-04-23 12:40 -0600
Message-ID<ato2sjFlcf5U1@mid.individual.net>
In reply to#19426
Evertjan said:

> Make your own function:
>
> function two(x){
>  return ('0' + x).substr(-2);
> };
>

Hi Evertjan:

    The function above on IE8 fails:    '10' is returned as '010'

    IE10, Chrome, and FF work properly..

-Mel Smith

[toc] | [prev] | [next] | [standalone]


#19429

FromChristoph Becker <cmbecker69@gmx.de>
Date2013-04-23 21:02 +0200
Message-ID<kl6lqm$3ej$1@speranza.aioe.org>
In reply to#19427
Mel Smith wrote:
> Evertjan said:
> 
>> Make your own function:
>>
>> function two(x){
>>  return ('0' + x).substr(-2);
>> };
>>
> 
>     The function above on IE8 fails:    '10' is returned as '010'
> 
>     IE10, Chrome, and FF work properly..

It is preferable to use String.prototype.slice() instead of
String.prototype.substr(); the former is standardized in ECMAScript 3rd
edition, section 15.5.4.13.[1]

[1]
<http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%201999.pdf>

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#19450

FromDr J R Stockton <reply1300@merlyn.demon.co.uk.invalid>
Date2013-04-24 22:12 +0100
Message-ID<EU$n+pH2qEeRFwOq@invalid.uk.co.demon.merlyn.invalid>
In reply to#19426
In comp.lang.javascript message <XnsA1ABB47255529eejj99@194.109.133.133>
, Tue, 23 Apr 2013 17:44:18, Evertjan. <exxjxw.hannivoort@inter.nl.net>
posted:

>Tony wrote on 23 apr 2013 in comp.lang.javascript:
>
>> Do I really have to manually use this kind of construction to always
>> use two digits for hour

No.

>> currentTime.setMinutes(currentTime.getMinutes() + 30);
>> hours = hours < 10 ? "0" + hours : hours;
>
>Make your own function:
>
>function two(x){
>  return ('0' + x).substr(-2);
>};
>
>now = new Date();
>myTime = two(now.getHours()) + ':' + two(now.getMinutes());
>alert(myTime);

Those Date methods can be relied on to give integer number results in
the range 0 to 99 inclusive, provided that the Object 'now' is a date
Object representing a valid date.  The result looks silly if the Object
holds NaN.

With a little thinking and testing, one can produce a sufficiently short
function which does what is wanted for inputs in the desired range and
gives appropriate non-misleading results for _all_ other inputs.
Perhaps

    function LZ(n) { return (n!=null&&n<10&&n>=0?"0":"") + n }


-- 
 (c) John Stockton, nr London, UK.   E-mail, see Home Page.    Turnpike v6.05.
 Website  <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
 PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
 Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.

[toc] | [prev] | [next] | [standalone]


#19459

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2013-04-25 09:39 +0200
Message-ID<XnsA1AD622F8E024eejj99@194.109.133.133>
In reply to#19450
Dr J R Stockton wrote on 24 apr 2013 in comp.lang.javascript:

>>Make your own function:
>>
>>function two(x){
>>  return ('0' + x).substr(-2);
>>};
>>
>>now = new Date();
>>myTime = two(now.getHours()) + ':' + two(now.getMinutes());
>>alert(myTime);
> 
> Those Date methods can be relied on to give integer number results in
> the range 0 to 99 inclusive, provided that the Object 'now' is a date

There is no chance that it is not a date, as I specified:

>> now = new Date();

> Object representing a valid date.  The result looks silly if the Object
> holds NaN.

> With a little thinking and testing, one can produce a sufficiently short
> function which does what is wanted for inputs in the desired range and
> gives appropriate non-misleading results for _all_ other inputs.
> Perhaps
> 
>     function LZ(n) { return (n!=null&&n<10&&n>=0?"0":"") + n }

or

function two(x) { return !isNaN(x) && x<10 && x>=0 ?"0" + x :x }

.. both loosing the educational symplicity of:

>> function two(x){
>>   return ('0' + x).substr(-2);
>> };

.. which seems not work for IE8- 

;-(

so try as advised in this thread:

function two(x){
  return ('0' + x).slice(-2);
};



-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#19497

FromDr J R Stockton <reply1300@merlyn.demon.co.uk.invalid>
Date2013-04-26 20:56 +0100
Message-ID<$NL54QElvteRFw5D@invalid.uk.co.demon.merlyn.invalid>
In reply to#19459
In comp.lang.javascript message <XnsA1AD622F8E024eejj99@194.109.133.133>
, Thu, 25 Apr 2013 09:39:07, Evertjan. <exxjxw.hannivoort@inter.nl.net>
posted:

>
>function two(x) { return !isNaN(x) && x<10 && x>=0 ?"0" + x :x }

Gives an inappropriate string if the argument is the null value.

The slicing methods are acceptable for in-range inputs, or if the input
may be larger but only the last two digits are wanted.

But consider   two(D.getMilliseconds()) ;  that will give the last two
digits, and the output will always look correct.  But if there has been
an editing mistake, converting a tested D.getSeconds() into
D.getMilliseconds(), the output of your function two will still always
look plausible, whereas that of my function LZ will, nine times out of
ten, show three digits - and that will ease detection of the *real*
error.   Etc.

>.. both loosing the educational symplicity of:

YSCIB.

-- 
 (c) John Stockton, nr London, UK.   E-mail, see Home Page.    Turnpike v6.05.
 Website  <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
 PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
 Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.

[toc] | [prev] | [next] | [standalone]


#19501

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2013-04-27 10:50 +0200
Message-ID<XnsA1AF6E3FF2DFeejj99@194.109.133.133>
In reply to#19497
Dr J R Stockton wrote on 26 apr 2013 in comp.lang.javascript:

> In comp.lang.javascript message <XnsA1AD622F8E024eejj99@194.109.133.133>
> , Thu, 25 Apr 2013 09:39:07, Evertjan. <exxjxw.hannivoort@inter.nl.net>
> posted:
> 
>>
>>function two(x) { return !isNaN(x) && x<10 && x>=0 ?"0" + x :x }

> Gives an inappropriate string if the argument is the null value.

How could you geve an appropriate string?
 
> The slicing methods are acceptable for in-range inputs, or if the input
> may be larger but only the last two digits are wanted.
> 
> But consider   two(D.getMilliseconds()) ;  that will give the last two
> digits, and the output will always look correct.  But if there has been
> an editing mistake, converting a tested D.getSeconds() into
> D.getMilliseconds(), the output of your function two will still always
> look plausible, whereas that of my function LZ will, nine times out of
> ten, show three digits - and that will ease detection of the *real*
> error.   Etc.

Good reasoning.

However, in this forum, I prefer educational symplicity to detection of 
programmatical errors.
 
>>.. both loosing the educational symplicity of:
> 
> YSCIB.

Eye yous noh essie.

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#19522

FromDr J R Stockton <reply1300@merlyn.demon.co.uk.invalid>
Date2013-04-28 16:49 +0100
Message-ID<5lq4ikDkUUfRFwCv@invalid.uk.co.demon.merlyn.invalid>
In reply to#19501
In comp.lang.javascript message <XnsA1AF6E3FF2DFeejj99@194.109.133.133>,
Sat, 27 Apr 2013 10:50:16, Evertjan. <exxjxw.hannivoort@inter.nl.net>
posted:

>Dr J R Stockton wrote on 26 apr 2013 in comp.lang.javascript:
>
>> In comp.lang.javascript message <XnsA1AD622F8E024eejj99@194.109.133.133>
>> , Thu, 25 Apr 2013 09:39:07, Evertjan. <exxjxw.hannivoort@inter.nl.net>
>> posted:
>>
>>>
>>>function two(x) { return !isNaN(x) && x<10 && x>=0 ?"0" + x :x }
>
>> Gives an inappropriate string if the argument is the null value.
>
>How could you geve an appropriate string?

Give the string "null", of course.

>However, in this forum, I prefer educational symplicity to detection of
>programmatical errors.

A superb example of being exactly wrong.

Remember - the vast majority of errors in programs & scripts are
unexpected, and finding them should never be made more difficult to
giving plausible but incorrect results.

You may recall the introduction to "Million Random Digits with 100, 000
Normal Deviates", Rand Corporation (Dec 1955).


-- 
 (c) John Stockton, Surrey, UK.  ¬@merlyn.demon.co.uk   Turnpike v6.05   MIME.
   Web  <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
 Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
 Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

[toc] | [prev] | [next] | [standalone]


#19524

FromStefan Weiss <krewecherl@gmail.com>
Date2013-04-29 01:19 +0200
Message-ID<klkaqt$jhk$1@news.albasani.net>
In reply to#19522
On 2013-04-28 17:49, Dr J R Stockton wrote:
> You may recall the introduction to "Million Random Digits with 100, 000
> Normal Deviates", Rand Corporation (Dec 1955).

Heh, I actually own a copy of this book. I can't remember the
introduction, and because the book is somewhere at the bottom of one of
my moving cardboard boxes, I can't look it up, but I vividly remember
the storyline -- fascinating stuff! Highly recommended for all pirate
romance mystery afficionados. I didn't know you were into that, Doc.

- stefan

[toc] | [prev] | [next] | [standalone]


#19526

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2013-04-29 09:05 +0200
Message-ID<XnsA1B15C7CC7F45eejj99@194.109.133.133>
In reply to#19522
Dr J R Stockton wrote on 28 apr 2013 in comp.lang.javascript:

> In comp.lang.javascript message
> <XnsA1AF6E3FF2DFeejj99@194.109.133.133>, Sat, 27 Apr 2013 10:50:16,
> Evertjan. <exxjxw.hannivoort@inter.nl.net> posted:
> 
>>Dr J R Stockton wrote on 26 apr 2013 in comp.lang.javascript:
>>
>>> In comp.lang.javascript message
>>> <XnsA1AD622F8E024eejj99@194.109.133.133> , Thu, 25 Apr 2013
>>> 09:39:07, Evertjan. <exxjxw.hannivoort@inter.nl.net> posted:
>>>
>>>>
>>>>function two(x) { return !isNaN(x) && x<10 && x>=0 ?"0" + x :x }
>>
>>> Gives an inappropriate string if the argument is the null value.
>>
>>How could you geve an appropriate string?
> 
> Give the string "null", of course.

Nonsense, "null" is not a string.

> 
>>However, in this forum, I prefer educational symplicity to detection
>>of programmatical errors.
> 
> A superb example of being exactly wrong.

Are you implying I do not prefer that?

> Remember - the vast majority of errors in programs & scripts are
> unexpected, and finding them should never be made more difficult to
> giving plausible but incorrect results.

That is not related to educational symplicity.

The best advice against making errors in programs & scripts by newbees is:
"Keep away from programming".

However, if we want to teach, actanig against this best advice,
we should give examples that are concise ans so easily understandable to 
them. 

An exposé of possible dangers and error pronenesses might be added if one 
is so inclined, but should never obscurate the pime working to the pupil.

> You may recall the introduction to "Million Random Digits with 100,
> 000 Normal Deviates", Rand Corporation (Dec 1955).

Then again, I may not. [no corporate rant intended] 

At that time my interests were more into networking: electrical, social
[!], wireless, and the obligatory resulting deviative randomness was 
presumably both always incidental and often even unnoticed.

Later, "white noice" became my ultimate goal in randomness, not trusting 
the pseudorandomness as delivered by formulae. A small chip using 
radioactive decay interval as a seeding source seemed the obvious choice.

New edition 2001:
<http://www.goodreads.com/book/show/292147.A_Million_Random_Digits_with_
100_000_Normal_Deviates>

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#19547

FromDr J R Stockton <reply1300@merlyn.demon.co.uk.invalid>
Date2013-04-30 22:14 +0100
Message-ID<+BT57qP$QDgRFwJs@invalid.uk.co.demon.merlyn.invalid>
In reply to#19526
In comp.lang.javascript message <XnsA1B15C7CC7F45eejj99@194.109.133.133>
, Mon, 29 Apr 2013 09:05:30, Evertjan. <exxjxw.hannivoort@inter.nl.net>
posted:

>Dr J R Stockton wrote on 28 apr 2013 in comp.lang.javascript:
>
>> In comp.lang.javascript message
>> <XnsA1AF6E3FF2DFeejj99@194.109.133.133>, Sat, 27 Apr 2013 10:50:16,
>> Evertjan. <exxjxw.hannivoort@inter.nl.net> posted:
>>
>>>Dr J R Stockton wrote on 26 apr 2013 in comp.lang.javascript:
>>>
>>>> In comp.lang.javascript message
>>>> <XnsA1AD622F8E024eejj99@194.109.133.133> , Thu, 25 Apr 2013
>>>> 09:39:07, Evertjan. <exxjxw.hannivoort@inter.nl.net> posted:
>>>>
>>>>>
>>>>>function two(x) { return !isNaN(x) && x<10 && x>=0 ?"0" + x :x }
>>>
>>>> Gives an inappropriate string if the argument is the null value.
>>>
>>>How could you geve an appropriate string?
>>
>> Give the string "null", of course.
>
>Nonsense, "null" is not a string.

Yes : "null" is four letters in quotation marks, the content of a
string; or, alternatively, is six characters which are a JavaScript
literal representation of a string.  The code
        alert(LZ(null))
causes a box containing four letters   n u l l   to appear.


>Later, "white noice" became my ultimate goal in randomness, not trusting
>the pseudorandomness as delivered by formulae. A small chip using
>radioactive decay interval as a seeding source seemed the obvious choice.

It is the obvious choice.  But some skill, thought, and experience is
needed to get a truly random sequence from what is necessarily part-
analogue hardware.  One must, for example, remember that the mean
interval between successive decays is itself decaying ... and that no
such product can work for ever.  Code such as that commended by Johannes
Baago/e is reliable and some of it gives an enormous cycle length.

Also, for testing, the random number sequence needs to be repeatable.


>New edition 2001:
><http://www.goodreads.com/book/show/292147.A_Million_Random_Digits_with_
>100_000_Normal_Deviates>

But is it a new edition of the digits, or merely the same 10^6 digits
repeated, which would be boring?  In any case, the wording of interest
in the old edition is not necessarily repeated in the new one.

-- 
 (c) John Stockton, nr London, UK.  For Mail, see Home Page.  Turnpike, WinXP.
 Web  <http://www.merlyn.demon.co.uk/> - FAQ-type topics, acronyms, and links.
 Command-prompt MiniTrue is useful for viewing/searching/altering files. Free,
 DOS/Win/UNIX now 2.0.6; see <URL:http://www.merlyn.demon.co.uk/pc-links.htm>.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web