I want to convert local datetime to unix timestamp(seconds) using Moment js. I have used following formulas:-
moment().unix() moment(new Date()).format("X")
Example:-
a) IST Time - 11th April 2020 11:45 AM , UNIX Timestamp - 1586605500 (In Seconds)
b) UTC Time - 11th April 2020 06:15 AM , UNIX Timestamp - 1586585700 (In Seconds)
I am expecting to get result a) as Unix Timestamp but I am getting b) instead. How to resolve this problem in easy way using Moment JS?
There's no such thing as a "local Unix timestamp". A Unix timestamp measures the number of seconds1 since the Unix epoch, which is defined as midnight on January 1st 1970 UTC.
This means that at any given instant in time, the current Unix timestamp is the same everywhere in the world. If people from two different countries were having a conversation, they'd both agree on "the current Unix timestamp" even if their watches showed different times.
So result b in your question is already correct: when it was 11:45am on April 11th 2020 in India, the Unix timestamp was 1586585700. Anything expecting 1586605500 isn't expecting a Unix timestamp. Rather than try to provide the result that that code expects, I would to correct that code to expect a Unix timestamp - or get it to expect a local value in some other form.
1 At least traditionally. The idea of "a Unix timestamp but in milliseconds, or microseconds, or nanoseconds" makes perfect sense, and doesn't change any of the rest of this answer.