Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

About format to always use 2 digits for hour

11 views
Skip to first unread message

Tony

unread,
Apr 23, 2013, 9:07:16 AM4/23/13
to
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

JJ

unread,
Apr 23, 2013, 9:55:13 AM4/23/13
to
You can string-extract the hour part of an ISO date.
It's simpler, but slower.

Jukka K. Korpela

unread,
Apr 23, 2013, 9:58:59 AM4/23/13
to
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/

Evertjan.

unread,
Apr 23, 2013, 11:44:18 AM4/23/13
to
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)

Mel Smith

unread,
Apr 23, 2013, 2:40:13 PM4/23/13
to
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


Christoph Becker

unread,
Apr 23, 2013, 3:02:17 PM4/23/13
to
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

Dr J R Stockton

unread,
Apr 24, 2013, 5:12:22 PM4/24/13
to
In comp.lang.javascript message <XnsA1ABB472...@194.109.133.133>
, Tue, 23 Apr 2013 17:44:18, Evertjan. <exxjxw.h...@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.

Evertjan.

unread,
Apr 25, 2013, 3:39:07 AM4/25/13
to
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);

Dr J R Stockton

unread,
Apr 26, 2013, 3:56:21 PM4/26/13
to
In comp.lang.javascript message <XnsA1AD622F...@194.109.133.133>
, Thu, 25 Apr 2013 09:39:07, Evertjan. <exxjxw.h...@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.

Evertjan.

unread,
Apr 27, 2013, 4:50:16 AM4/27/13
to
Dr J R Stockton wrote on 26 apr 2013 in comp.lang.javascript:

> In comp.lang.javascript message <XnsA1AD622F...@194.109.133.133>
> , Thu, 25 Apr 2013 09:39:07, Evertjan. <exxjxw.h...@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.

Dr J R Stockton

unread,
Apr 28, 2013, 11:49:56 AM4/28/13
to
In comp.lang.javascript message <XnsA1AF6E3...@194.109.133.133>,
Sat, 27 Apr 2013 10:50:16, Evertjan. <exxjxw.h...@inter.nl.net>
posted:

>Dr J R Stockton wrote on 26 apr 2013 in comp.lang.javascript:
>
>> In comp.lang.javascript message <XnsA1AD622F...@194.109.133.133>
>> , Thu, 25 Apr 2013 09:39:07, Evertjan. <exxjxw.h...@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)

Stefan Weiss

unread,
Apr 28, 2013, 7:19:56 PM4/28/13
to
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

Evertjan.

unread,
Apr 29, 2013, 3:05:30 AM4/29/13
to
Dr J R Stockton wrote on 28 apr 2013 in comp.lang.javascript:

> In comp.lang.javascript message
> <XnsA1AF6E3...@194.109.133.133>, Sat, 27 Apr 2013 10:50:16,
> Evertjan. <exxjxw.h...@inter.nl.net> posted:
>
>>Dr J R Stockton wrote on 26 apr 2013 in comp.lang.javascript:
>>
>>> In comp.lang.javascript message
>>> <XnsA1AD622F...@194.109.133.133> , Thu, 25 Apr 2013
>>> 09:39:07, Evertjan. <exxjxw.h...@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>

Dr J R Stockton

unread,
Apr 30, 2013, 5:14:39 PM4/30/13
to
In comp.lang.javascript message <XnsA1B15C7C...@194.109.133.133>
, Mon, 29 Apr 2013 09:05:30, Evertjan. <exxjxw.h...@inter.nl.net>
posted:

>Dr J R Stockton wrote on 28 apr 2013 in comp.lang.javascript:
>
>> In comp.lang.javascript message
>> <XnsA1AF6E3...@194.109.133.133>, Sat, 27 Apr 2013 10:50:16,
>> Evertjan. <exxjxw.h...@inter.nl.net> posted:
>>
>>>Dr J R Stockton wrote on 26 apr 2013 in comp.lang.javascript:
>>>
>>>> In comp.lang.javascript message
>>>> <XnsA1AD622F...@194.109.133.133> , Thu, 25 Apr 2013
>>>> 09:39:07, Evertjan. <exxjxw.h...@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>.
0 new messages