1

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

  • what did you try yourself to achieve this? – Akber Iqbal Dec 03 '18 at 06:20
  • @AIqbalRaj - upto now i tried the below code but for some reason it is not work TimeSlotDetails.sort(function compare(a, b) { return moment(moment.utc(a.StartTime).tz("CST").format("HH:mm"),"HH:mm").isBefore(moment(moment.utc(b.StartTime).tz("CST").format("HH:mm"),"HH:mm")); }) – Vishmay Shah Dec 03 '18 at 06:35

1 Answers1

2

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.
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Thanks matt!, Moment .duration will help me much but There are a few points. 1. The "CST" is my custom timezone,we can setup it in moment js at initialization. 2. the idea of comparing duration seems good but i want to convert the timespan "UTC" to "CST" before comparing in other timezone. so i will need moment object, convert it and now i want to extract out the updated duration of time from the moment object. 3. I've time in timespan format PT4H5M6S available with me in source timezone(in UTC). Thanks! – Vishmay Shah Dec 05 '18 at 04:54
  • I agree with time zones are completely unrelated to time spans. but i've time span in utc and i need equivalent in cst(as i need to show in multi timezone environment). – Vishmay Shah Dec 05 '18 at 05:38
  • Again, that makes no sense. Your `PT4H5M6S` means literally, "four hours, five minutes, and six seconds". It does *not* mean "4:05:06 AM". You can't have a timespan in UTC, or in CST, or in any other time zone. In the real world, a timespan is what comes out of a stopwatch, like when you're measuring a runner in a race, or how long your pasta is cooking for. It is a very different instrument than an analog clock that hangs on a wall to tell students when class is over, or the digital clock that might sit by your bedside to wake you up in the morning. – Matt Johnson-Pint Dec 05 '18 at 18:27
  • Yiu actually *can't* just think of it as time at midnight. This is a common fallacy.. See point 1b in my answer. See [DST in Brazil](https://www.timeanddate.com/time/change/brazil/sao-paulo) for example of a day that might not have a midnight. – Matt Johnson-Pint Dec 07 '18 at 05:19
  • I'm understanding the point you are saying about timespan is not time. But if we consider it as time starting from today's 00:00 it can mean 4:05:06 AM. and yes there are many cases i can use time span as time of day. let me explain my requirements more cleanly. I want to save my routine task list in weekdays. Like in Sunday 7:00 to 8:00 AM i want to attend a class. and now i want to take input for what i did on that day like on 9-dec-2018, 16-dec-2018, 7:00 to 8:00 am So here i will save the time in timespan in my DB and append it on given date in ui. – Vishmay Shah Dec 07 '18 at 05:39
  • I hear you. But time is quite complex. Consider the example of the class that starts at 7:00 AM. Say the date was on 2018-11-04, and the class was in the parts of the US that use daylight saving time (which is most of the US) - say Chicago (Central Time). While the local time might be 7:00 AM, there would have actually been 8 hours elapsed since 00:00, because the hour from 1:00 - 1:59 has been repeated. – Matt Johnson-Pint Dec 07 '18 at 18:07
  • Also then consider if for some reason the class actually was in the middle of the night, say 1:30 AM. How will you know if it is the *first* occurrence (1:30 AM CDT) or the *second* occurrence (1:30 AM CST)? Scheduling is difficult. It's not quite as simple as you might think. There are lots of questions on StackOverflow on this subject already, but your question here was specific to Moment and its API, and not to the overall process of scheduling. I suggest starting [here](https://stackoverflow.com/a/19627330/634824) and searching for others as well. Good luck! :) – Matt Johnson-Pint Dec 07 '18 at 18:09