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


Groups > comp.lang.javascript > #25856

Re: Ordering time stamp

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!news2.arglkargh.de!news.swapon.de!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.javascript
Subject Re: Ordering time stamp
Date Mon, 15 Sep 2014 13:55:21 +0100
Organization A noiseless patient Spider
Lines 41
Message-ID <87egvd3ux2.fsf@bsb.me.uk> (permalink)
References <PMKdnU_uWMGj9IvJnZ2dnUVZ_u2dnZ2d@westnet.com.au>
Mime-Version 1.0
Content-Type text/plain
Injection-Info mx05.eternal-september.org; posting-host="017616aa25f81ec581c44d76d61ba2f3"; logging-data="7803"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/T7Ah4uZf95WWqYw8PM//fT3vAjRTYoAw="
Cancel-Lock sha1:Ct1gUs1zBlCUtgsQ3X+1QkbFaj0= sha1:VnRE0/yJXw1mHi2mr46mg7RFTqg=
X-BSB-Auth 1.be85204d4ec84bc81bb5.20140915135521BST.87egvd3ux2.fsf@bsb.me.uk
Xref csiph.com comp.lang.javascript:25856

Show key headers only | View raw


Andrew Poulos <ap_prog@hotmail.com> writes:

> A piece of elearning I'm working generates JSON as an array of objects
> (from a "new" standard called Tin Can). Each object has a timestamp
> property.
>
> How do I order the array from the time stamp where a time stamp looks
> like one of these:
>   timestamp: "2014-09-15T03:27:16.017Z"
>   timestamp: "2014-09-15T03:27:17.023Z"
>   timestamp: "2014-09-15T03:27:17.029Z"
>
> The standard's example js shows the generation of a time stamp with this
>
> if (this.timestamp === null) {
>   this.timestamp = TinCan.Utils.getISODateString(new Date());
> }
>
> Can I simply do something based on (for example):
>
> var x = new Date("2014-09-15T03:27:16.017Z");
> var y = new Date("2014-09-15T03:27:17.023Z");
>
> if (x > y) {
>   // x is newer
> } else {
>   // y is newer
> }

Yes, but that hides the conversion that's being done.  It may be better
to explicitly compare the times so you don't get a surprise when using
==.

  if (x.getTime() > y.getTime())

However, if the timestamps are always in the form shown (and you don't
need the Date objects for some other reason), you could just compare the
strings without making Date objects from them.

-- 
Ben.

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


Thread

Ordering time stamp Andrew Poulos <ap_prog@hotmail.com> - 2014-09-15 14:17 +1000
  Re: Ordering time stamp Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-09-15 13:55 +0100
  Re: Ordering time stamp Denis McMahon <denismfmcmahon@gmail.com> - 2014-09-16 02:30 +0000
    Re: Ordering time stamp Andrew Poulos <ap_prog@hotmail.com> - 2014-09-16 13:29 +1000
      Re: Ordering time stamp Denis McMahon <denismfmcmahon@gmail.com> - 2014-09-16 14:46 +0000

csiph-web