Travis Williamson
Travis Williamson

Reputation: 7

Trouble subtracting time from a moment

I am working on a project that uses a different timezone than the one I currently live in:

moment.tz.add("Asia/Seoul|LMT KST JST KST KDT KDT|-8r.Q -8u -90 -90 -9u -a0|0123141414141414135353|-2um8r.Q 97XV.Q 1m1zu kKo0 2I0u OL0 1FB0 Rb0 1qN0 TX0 1tB0 TX0 1tB0 TX0 1tB0 TX0 2ap0 12FBu 11A0 1o00 11A0|23e6")

Then formatted it as such: const kST = moment().tz('Asia/Seoul').format("HH:mm");

Now I want to be able to subtract a year from that. Looking at examples of how to do that I found something like var foo = moment(blah, "HH:mm).subtract(1, "years")

I assumed that since I already defined the moment with the 'const kST' I could simply substitute "kST" for "moment" as such:

const firstTimeConverted = kST(firstBus, "HH:mm").subtract(1, "years");

Unfortunately that doesn't work. Any thoughts how I might do this would be much appreciated.

Upvotes: 0

Views: 23

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241525

In your code, kST isn't a function - it's a string. The format returns a string formatted as specified.

You probably want:

const firstTimeConverted = moment.tz(firstBus, "HH:mm", `Asia/Seoul`).subtract(1, "years");

This will parse the string in your firstBus variable in HH:mm format (such as "23:45"), and interpret as belonging to the Asia/Seoul time zone on the current date there. Then it will subtract a year and return the result as a moment object. If you want a string, you would then need to call the format function.

Upvotes: 1

Related Questions