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


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

About extracting the right format from Date

Started by"Tony" <tony.johansson@inport.com>
First post2013-04-29 18:09 +0200
Last post2013-05-02 10:17 +0200
Articles 14 — 5 participants

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


Contents

  About extracting the right format from Date "Tony" <tony.johansson@inport.com> - 2013-04-29 18:09 +0200
    Re: About extracting the right format from Date "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-04-29 18:38 +0200
      Re: About extracting the right format from Date "Tony" <tony.johansson@inport.com> - 2013-04-29 19:04 +0200
        Re: About extracting the right format from Date "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-04-29 20:50 +0200
          Re: About extracting the right format from Date "Tony" <tony.johansson@inport.com> - 2013-04-30 08:01 +0200
    Re: About extracting the right format from Date Bart Van der Donck <bart@nijlen.com> - 2013-04-30 04:41 -0700
      Re: About extracting the right format from Date Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-04-30 21:51 +0200
        Re: About extracting the right format from Date Bart Van der Donck <bart@nijlen.com> - 2013-05-02 00:44 -0700
          Re: About extracting the right format from Date Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-02 14:17 +0200
            Re: About extracting the right format from Date Bart Van der Donck <bart@nijlen.com> - 2013-05-02 06:46 -0700
      Re: About extracting the right format from Date Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-05-01 20:14 +0100
        Re: About extracting the right format from Date Bart Van der Donck <bart@nijlen.com> - 2013-05-02 01:09 -0700
          Re: About extracting the right format from Date "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-05-02 10:39 +0200
        Re: About extracting the right format from Date "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-05-02 10:17 +0200

#19531 — About extracting the right format from Date

From"Tony" <tony.johansson@inport.com>
Date2013-04-29 18:09 +0200
SubjectAbout extracting the right format from Date
Message-ID<klm5qa$379$1@dont-email.me>
When I do this is the stored value in timeArray[i]  Mon Apr 22 20:00:00 
UTC+0200 2013
timeArray[i] = new Date(tempDate.valueOf());

So is there a simple way to set format to this instead
2013/04/22 20:00

Or do I have to extract and set explicitly the value that I want to have

//Tony

[toc] | [next] | [standalone]


#19532

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2013-04-29 18:38 +0200
Message-ID<XnsA1B1BD99DBA64eejj99@194.109.133.133>
In reply to#19531
Tony wrote on 29 apr 2013 in comp.lang.javascript:

> When I do this is the stored value in timeArray[i]  Mon Apr 22 20:00:00 
> UTC+0200 2013
> timeArray[i] = new Date(tempDate.valueOf());
> 
> So is there a simple way to set format to this instead
> 2013/04/22 20:00

Yes, just convert the different parts to string,
as shown in the recent thread.
 
> Or do I have to extract and set explicitly the value that I want to have

How much simpler can it be?

========================

However, it the array is just for storage and retrieval, why not put the 
total date value there, instead of reinventing the wheel?

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

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


#19533

From"Tony" <tony.johansson@inport.com>
Date2013-04-29 19:04 +0200
Message-ID<klm90o$nee$1@dont-email.me>
In reply to#19532

"Evertjan."  wrote in message news:XnsA1B1BD99DBA64eejj99@194.109.133.133...

Tony wrote on 29 apr 2013 in comp.lang.javascript:

> When I do this is the stored value in timeArray[i]  Mon Apr 22 20:00:00
> UTC+0200 2013
> timeArray[i] = new Date(tempDate.valueOf());
>
> So is there a simple way to set format to this instead
> 2013/04/22 20:00

Yes, just convert the different parts to string,
as shown in the recent thread.

> Or do I have to extract and set explicitly the value that I want to have

How much simpler can it be?

========================

However, it the array is just for storage and retrieval, why not put the
total date value there, instead of reinventing the wheel?

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

Yes I can store the Date in the array but there is another problem
I compare the value that have this  format Mon Apr 22 20:00:00 UTC+02:00 
2013
with another value that look like this
2013/04/22 21:10

The only way as I see this if I would be able to compare is to
create a new date and store this value 2013/04/22 21:10.

Or is it a better way ?

//tony

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


#19535

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2013-04-29 20:50 +0200
Message-ID<XnsA1B1D413D28F5eejj99@194.109.133.133>
In reply to#19533
Tony wrote on 29 apr 2013 in comp.lang.javascript:

> However, it the array is just for storage and retrieval, why not put the
> total date value there, instead of reinventing the wheel?
> 
> -- 
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)
> 
> Yes I can store the Date in the array but there is another problem
> I compare the value that have this  format Mon Apr 22 20:00:00 UTC+02:00 
> 2013
> with another value that look like this
> 2013/04/22 21:10
> 
> The only way as I see this if I would be able to compare is to
> create a new date and store this value 2013/04/22 21:10.
> 

You were recently warned [I believe] not to write under the signature, 
as it is nearly unreadable with conforming usenet readers.

> Or is it a better way ?

What is?

"better" is in the eye of the individual, but quite so "there" is.

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

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


#19540

From"Tony" <tony.johansson@inport.com>
Date2013-04-30 08:01 +0200
Message-ID<klnmhd$n3m$1@dont-email.me>
In reply to#19535

"Evertjan."  wrote in message news:XnsA1B1D413D28F5eejj99@194.109.133.133...

Tony wrote on 29 apr 2013 in comp.lang.javascript:

> However, it the array is just for storage and retrieval, why not put the
> total date value there, instead of reinventing the wheel?
>
> -- 
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)
>
> Yes I can store the Date in the array but there is another problem
> I compare the value that have this  format Mon Apr 22 20:00:00 UTC+02:00
> 2013
> with another value that look like this
> 2013/04/22 21:10
>
> The only way as I see this if I would be able to compare is to
> create a new date and store this value 2013/04/22 21:10.
>

You were recently warned [I believe] not to write under the signature,
as it is nearly unreadable with conforming usenet readers.

> Or is it a better way ?

What is?

"better" is in the eye of the individual, but quite so "there" is.

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

I fixed it by extracting the date and time part and create a new data object

//Tony 

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


#19542

FromBart Van der Donck <bart@nijlen.com>
Date2013-04-30 04:41 -0700
Message-ID<0073691b-8f9e-4b11-9431-7759339ed68b@googlegroups.com>
In reply to#19531
Tony wrote:

> When I do this is the stored value in timeArray[i]  
> Mon Apr 22 20:00:00 UTC+0200 2013 
> timeArray[i] = new Date(tempDate.valueOf());

Are you sure of that format ? The ouput of 'new Date()' may differ. Eg. on my system:

  MSIE: Tue Apr 30 11:56:01 UTC+0200 2013 
  Opera: Tue, 30 Apr 2013 11:56:01 GMT+0200
  SeaMonkey: Tue Apr 30 2013 11:56:01 GMT+0200 (Romance (zomertijd))
  Firefox: Tue Apr 30 2013 11:56:01 GMT+0200 (Romance (zomertijd)) 
  Chrome: Tue Apr 30 2013 11:56:01 GMT+0200 (Romance (zomertijd))

> So is there a simple way to set format to this instead
> 2013/04/22 20:00

  var d = new Date();
  alert( d.getFullYear() 
         + '/'
         + ('0' + (1 + d.getMonth())).slice(-2)
         + '/'
         + ('0' + d.getDate()).slice(-2)
         + ' '
         + ('0' + d.getHours()).slice(-2)
         + ':'
         + ('0' + d.getMinutes()).slice(-2)
       ); 

> Or do I have to extract and set explicitly the value that I 
> want to have 

The most solid is probably to use the various methods of the Date()-object.

Hope this helps,

-- 
 Bart

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


#19544

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-04-30 21:51 +0200
Message-ID<5064953.7cZPh7B9jv@PointedEars.de>
In reply to#19542
Bart Van der Donck wrote:

> Tony wrote:
>> When I do this is the stored value in timeArray[i]
>> Mon Apr 22 20:00:00 UTC+0200 2013
>> timeArray[i] = new Date(tempDate.valueOf());
> 
> Are you sure of that format ? The ouput of 'new Date()' may differ. Eg. on
> my system:
> 
>   MSIE: Tue Apr 30 11:56:01 UTC+0200 2013
>   Opera: Tue, 30 Apr 2013 11:56:01 GMT+0200
>   SeaMonkey: Tue Apr 30 2013 11:56:01 GMT+0200 (Romance (zomertijd))
>   Firefox: Tue Apr 30 2013 11:56:01 GMT+0200 (Romance (zomertijd))
>   Chrome: Tue Apr 30 2013 11:56:01 GMT+0200 (Romance (zomertijd))

What you are seeing there is only the implementation-dependent *string 
representation* of the referred Date instance and its internal time value.  

It does not matter here, though, assuming that “tempDate” refers to another 
Date instance.  The ECMAScript Language Specification, from its first 
Edition, specifies that the default valueOf() method of a Date instance, as 
inherited from Date.prototype, returns the time value of that instance as a 
primitive Number value.  And the Date constructor is specified to create a 
Date instance with the first argument as time value if the first argument is 
a Number (as returned by Date.prototype.valueOf() by default).  So this is 
actually the easiest way to clone/restore a Date instance.
 
>> So is there a simple way to set format to this instead
>> 2013/04/22 20:00
> 
>   var d = new Date();
>   alert( d.getFullYear()
>          + '/'
>          + ('0' + (1 + d.getMonth())).slice(-2)
>          + '/'
>          + ('0' + d.getDate()).slice(-2)
>          + ' '
>          + ('0' + d.getHours()).slice(-2)
>          + ':'
>          + ('0' + d.getMinutes()).slice(-2)
>        );

Unfortunately, although that approach is quite elegant, calling 
String.prototype.slice() with negative arguments is not interoperable.
 
>> Or do I have to extract and set explicitly the value that I
>> want to have
> 
> The most solid is probably to use the various methods of the
> Date()-object.

ACK.  However, it is practical to have at least one date formatting method.

See also: <http://PointedEars.de/scripts/test/date>

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

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


#19570

FromBart Van der Donck <bart@nijlen.com>
Date2013-05-02 00:44 -0700
Message-ID<a07bfcc1-39e4-4e37-8a9d-6e82608fe8a5@googlegroups.com>
In reply to#19544
Thomas 'PointedEars' Lahn wrote:

> Bart Van der Donck wrote:
>
>> MSIE: Tue Apr 30 11:56:01 UTC+0200 2013
>> Opera: Tue, 30 Apr 2013 11:56:01 GMT+0200
>> SeaMonkey: Tue Apr 30 2013 11:56:01 GMT+0200 (Romance (zomertijd))
>> Firefox: Tue Apr 30 2013 11:56:01 GMT+0200 (Romance (zomertijd))
>> Chrome: Tue Apr 30 2013 11:56:01 GMT+0200 (Romance (zomertijd))
> 
> What you are seeing there is only the implementation-dependent 
> *string representation* of the referred Date instance and its 
> internal time value.  
> It does not matter here, though, assuming that “tempDate” refers 
> to another Date instance.  The ECMAScript Language Specification, 
> from its first Edition, specifies that the default valueOf() 
> method of a Date instance, as inherited from Date.prototype, 
> returns the time value of that instance as a primitive Number 
> value.  And the Date constructor is specified to create a Date 
> instance with the first argument as time value if the first 
> argument is a Number (as returned by Date.prototype.valueOf() 
> by default).  So this is actually the easiest way to clone/restore
> a Date instance.

Yes. Looks like I was under the impression that the OP would further parse the string representation of Date().

>>  var d = new Date(); 
>>  alert( d.getFullYear() 
>>         + '/' 
>>         + ('0' + (1 + d.getMonth())).slice(-2)
>>         + '/'
>>         + ('0' + d.getDate()).slice(-2)
>>         + ' '
>>         + ('0' + d.getHours()).slice(-2)
>>         + ':'
>>         + ('0' + d.getMinutes()).slice(-2)
>>       );
>  
> Unfortunately, although that approach is quite elegant, 
> calling String.prototype.slice() with negative arguments 
> is not interoperable.

There is no problem whatsoever to use 'slice()' with a negative number.

-- 
 Bart

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


#19577

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-05-02 14:17 +0200
Message-ID<2031344.OscmL0gD8O@PointedEars.de>
In reply to#19570
Bart Van der Donck wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Bart Van der Donck wrote:
>>>  var d = new Date();
>>>  alert( d.getFullYear()
>>>         + '/'
>>>         + ('0' + (1 + d.getMonth())).slice(-2)
>>>         + '/'
>>>         + ('0' + d.getDate()).slice(-2)
>>>         + ' '
>>>         + ('0' + d.getHours()).slice(-2)
>>>         + ':'
>>>         + ('0' + d.getMinutes()).slice(-2)
>>>       );
>>  
>> Unfortunately, although that approach is quite elegant,
>> calling String.prototype.slice() with negative arguments
>> is not interoperable.
> 
> There is no problem whatsoever to use 'slice()' with a negative number.

None that you are aware of.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

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


#19581

FromBart Van der Donck <bart@nijlen.com>
Date2013-05-02 06:46 -0700
Message-ID<074e980c-def0-455e-a4ee-8564bf614ee9@googlegroups.com>
In reply to#19577
Thomas 'PointedEars' Lahn wrote:

> Bart Van der Donck wrote:
> 
>> Thomas 'PointedEars' Lahn wrote:
>> 
>>> Unfortunately, although that approach is quite elegant,
>>> calling String.prototype.slice() with negative arguments
>>> is not interoperable.
>> 
>> 
>> There is no problem whatsoever to use 'slice()' with a 
>> negative number.
> 
> None that you are aware of.

I can't find any evidence why 'slice()' with a negative index would be problematic. Care to backup your claim ?

-- 
 Bart

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


#19560

FromDr J R Stockton <reply1300@merlyn.demon.co.uk.invalid>
Date2013-05-01 20:14 +0100
Message-ID<+FQ5KyIymWgRFwZ5@invalid.uk.co.demon.merlyn.invalid>
In reply to#19542
In comp.lang.javascript message <0073691b-8f9e-4b11-9431-7759339ed68b@go
oglegroups.com>, Tue, 30 Apr 2013 04:41:18, Bart Van der Donck
<bart@nijlen.com> posted:

>  var d = new Date();
>  alert( d.getFullYear()
>         + '/'
>         + ('0' + (1 + d.getMonth())).slice(-2)
>         + '/'
>         + ('0' + d.getDate()).slice(-2)
>         + ' '
>         + ('0' + d.getHours()).slice(-2)
>         + ':'
>         + ('0' + d.getMinutes()).slice(-2)
>       );


Bad coding.  Padding to two digits should be done with a function or
method, not by repeating code.

Instead of than prepending '0', one can add 100; I do not know which is
currently faster on the average.

-- 
 (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]


#19572

FromBart Van der Donck <bart@nijlen.com>
Date2013-05-02 01:09 -0700
Message-ID<ab04ee51-8d2a-4910-ad1f-f4bbc456d7e3@googlegroups.com>
In reply to#19560
Dr J R Stockton wrote:

> Bart Van der Donck <bart@nijlen.com> posted:
> 
>>  var d = new Date();
>>  alert( d.getFullYear()
>>        + '/'
>>        + ('0' + (1 + d.getMonth())).slice(-2)
>>        + '/'
>>        + ('0' + d.getDate()).slice(-2)
>>        + ' '
>>        + ('0' + d.getHours()).slice(-2)
>>        + ':'
>>        + ('0' + d.getMinutes()).slice(-2)
>>      );
> 
> Bad coding.  Padding to two digits should be done with a 
> function or method, not by repeating code.

http://en.wikipedia.org/wiki/Code_smell
Code duplication is the first in line :)
I think the real problem is the obligatory workaround trip; a programmer can't set the desired leading/trailing zeroes in javascript itself. This is a serious shortcoming in my opinion; it's a quite common need in daily programming, and exists in all other computer languages that I've used so far.
 
> Instead of than prepending '0', one can add 100; I do not 
> know which is currently faster on the average.

I would expect +100 because of its sheer arithmetic nature.

-- 
 Bart

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


#19574

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2013-05-02 10:39 +0200
Message-ID<XnsA1B46C5C2A405eejj99@194.109.133.133>
In reply to#19572
Bart Van der Donck wrote on 02 mei 2013 in comp.lang.javascript:

>> Instead of than prepending '0', one can add 100; I do not 
>> know which is currently faster on the average.
> 
> I would expect +100 because of its sheer arithmetic nature.
> 

However, since [explicit!] toString() is needed,
slice() does not accept a numeric value,
the difference could be marginal to none, Bart.

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

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


#19573

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2013-05-02 10:17 +0200
Message-ID<XnsA1B468A24C9C2eejj99@194.109.133.133>
In reply to#19560
Dr J R Stockton wrote on 01 mei 2013 in comp.lang.javascript:

> In comp.lang.javascript message <0073691b-8f9e-4b11-9431-7759339ed68b@go
> oglegroups.com>, Tue, 30 Apr 2013 04:41:18, Bart Van der Donck
> <bart@nijlen.com> posted:
> 
>>  var d = new Date();
>>  alert( d.getFullYear()
>>         + '/'
>>         + ('0' + (1 + d.getMonth())).slice(-2)
>>         + '/'
>>         + ('0' + d.getDate()).slice(-2)
>>         + ' '
>>         + ('0' + d.getHours()).slice(-2)
>>         + ':'
>>         + ('0' + d.getMinutes()).slice(-2)
>>       );
> 
> 
> Bad coding.  Padding to two digits should be done with a function or
> method, not by repeating code.
> 
> Instead of than prepending '0', one can add 100; I do not know which is
> currently faster on the average.

Repeated "adding" [=concatenation] of strings is also to be discouraged,
because of inferior performance.

Try String.concat():

=================================
alert( formatter( new Date() ) );

function formatter(d){ 
   return ''
   .concat(
     d.getFullYear().toString(),
     '/',
     two(1 + d.getMonth()),
     '/',
     two(d.getDate()),
     ' ',
     two(d.getHours()),
     ':',
     two(d.getMinutes()) 
   );
};

function two(x) {
   return (100+x).toString().slice(-2);
};
=================================

or Array.join():

=================================
alert( formatter( new Date() ) );

function formatter(d){ 
   return [
     d.getFullYear(),
     '/',
     two(1 + d.getMonth()),
     '/',
     two(d.getDate()),
     ' ',
     two(d.getHours()),
     ':',
     two(d.getMinutes())
   ].join('');
};

function two(x) {
   return (100+x).toString().slice(-2);
};
=================================


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

[toc] | [prev] | [standalone]


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


csiph-web