Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #19459
| Newsgroups | comp.lang.javascript |
|---|---|
| Subject | Re: About format to always use 2 digits for hour |
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
| References | <kl60sb$9nn$1@dont-email.me> <XnsA1ABB47255529eejj99@194.109.133.133> <EU$n+pH2qEeRFwOq@invalid.uk.co.demon.merlyn.invalid> |
| Date | 2013-04-25 09:39 +0200 |
| Message-ID | <XnsA1AD622F8E024eejj99@194.109.133.133> (permalink) |
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)
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web