-2

I have time1 = '09:00 AM' and time2 = '06:30 PM'. How can i subtract these two using moment.js, such a way that i get the result = 9hrs 30mins.

I searched through the internet but couldn't find an apt solution. Any suggestions are much appreciated.

sumesh
  • 2,179
  • 2
  • 21
  • 37
  • 3
    https://momentjs.com/docs/#/manipulating/subtract/ ? – taylorc93 Apr 29 '17 at 11:53
  • https://momentjs.com/docs/#/displaying/difference/ – Sayan Pal Apr 29 '17 at 11:56
  • That's just not a feature Moment offers. It's trivial for you to build it *using* Moment... (@SayanPal - `difference` doesn't produce that output.) – T.J. Crowder Apr 29 '17 at 11:56
  • check it [time difference in momentjs](http://stackoverflow.com/questions/29745873/hour-difference-between-two-timeshhmmss-ain-momentjs) – ShivamRajput Apr 29 '17 at 11:58
  • 2
    Possible duplicate of [Hour difference between two times(HH:MM:SS a)in momentjs](http://stackoverflow.com/questions/29745873/hour-difference-between-two-timeshhmmss-ain-momentjs) – Soren Apr 29 '17 at 12:04

1 Answers1

0

I suggest programming your own custom function that converts the time to 24 hrs, adding 12 hours if PM, 0 if AM. It then converts the times in to minutes, subtracts the two times, and converts them back into HH:MM AM/PM.

(Pseudocode (reads a bit like javascript))

//ap = am or pm; 0 for am, 1 for pm
define "subtractTimes" (time1, time1ap, time2, time2ap):

    //gets the length of time1 and time2
    set "time1Length" to (length(time1))
    set "time2Length" to (length(time2))

    //adds 12 hours if pm, converted to minutes
    set "time1InMinutes" to (time1ap * (12 * 60))
    set "time2InMinutes" to (time2ap * (12 * 60))

    //gets minutes from time1 and time2
    //You'd have to program your own function "getLetters"
    //Unless there's one that I'm unaware of. 
    set "time1MM" to (getLetters(time1,(time1Length-1),time1Length))
    set "time2MM" to (getLetters(time2,(time2Length-1),time2Length))


    //this script makes sure the times are the proper length.
    if "time1Length" = (4)
        set "time1" to (("0")join(time1)
    if "time2Length" = (4)
        set "time2" to (("0")join(time2)

    //gets hours from time1 and time 2
    set "time1HH" to (getLetters(time1,(time1Length-4),time1Length-3))
    set "time2HH" to (getLetters(time2,(time2Length-4),time2Length-3))

    //puts it all together
    set "time1InMinutes" to (time1InMinutes+(time1HH*60)+time1MM)
    set "time2InMinutes" to (time2InMinutes+(time2HH*60)+time2MM)
    set "newTimeInMinutes" to ((time1InMinutes)-(time2InMinutes))

    //converts it to HH:MM AM/PM
    set "newTime" to (floor(newTimeInMinutes/60))
    set newTimeInMinutes" to (newTimeInMinutes-(newTime*60))
    if "newTime" > (12): 
        set "newTime" to (newTime-12)
        set "ap" to (AM)
    else
        set "ap" to (PM)
    set "newTime" to ((newTime)join(":")join(newTimeInMinutes))
    set "newTime" to ((newTime)join(" ")join(ap)

    //end

This should work. If you have any questions, feel free to ask.

I. C.
  • 61
  • 1
  • 3