Vishmay Shah
Vishmay Shah

Reputation: 13

How to compare timespan in javascript after timezone conversion using moment js

I've a list of timespan(object list actually), like 2:00, 15:00, 18:00 etc, it is in utc. Now i want to convert this time slot back to CST and then sort it, as i want my time sorted in cst.

For timezone conversion i needed temporary date. so i choose current utc date by moment.utc(mytimespan). and performed the timezone conversion by .tz("CST").

So list is converted to 20:00,9:00, 12:00

Here please note that i got 20:00 in first place instead of last place in the list. This is due to date part of moment which went in back date.

All here i want is my timespan in sorted form without any effect of date.

please me to find a way to do it without string conversion!

Thanks

Update

my currently working code using string conversion

TimeSpanDetails.sort(function compare(a, b) {
  return moment(moment.utc(a.startTime).tz("CST").format("HH:mm"),"HH:mm").isAfter(moment(moment.utc(b.startTime).tz("CST").format("HH:mm"),"HH:mm")) ? 1 : -1;
});

Now i want to do it without string conversion using format

Upvotes: 1

Views: 1335

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241563

A few things:

  • A "time span" usually refers to a duration of time, not a time-of-day. These are two very different concepts that are sometimes confused. Consider:

    • A timespan of 99 hours is perfectly valid, but "99:00" is nonsensical as a time-of-day.
    • Due to daylight saving time and other time zone transitions, a timespan can't necessarily be thought of as "time since midnight" because midnight may or may not exist, or some other hour of the day may be absent or repeated.
    • Time spans can be negative in some programing languages, usually representing a period before a given point in time.
  • The tz function in Moment.js takes IANA time zone names. You should not use CT or CST, but rather America/Chicago, for example. However, time zones are completely unrelated to time spans, so you should not be applying them at all. You do not need moment-timezone.

  • Moment represents time spans in Duration objects. You can parse them from strings like so:

    var d = moment.duration('99:00');
    

    Duration objects convert numerically to milliseconds, so they are comparable like so:

    var a = moment.duration('00:00');
    var b = moment.duration('01:00');
    var c = a < b;   //=> true
    
  • Moment does not have a strongly typed object for a time-of-day, but you can use Moment in UTC mode so that it does not have DST transitions, and then just let it use the current day. HOWEVER:

    • This would assume that all time-of-day values you have should be evaluated on the same date. This may or may not be the case.
    • Consider that if all you have is time-of-day and don't know what dates they're from, then the values ['23:00', '00:00'] may be sorted already and only one hour apart, or perhaps they're out of sequence and they are 23 hours apart.

Upvotes: 2

Related Questions