Anjana
Anjana

Reputation: 55

Moment.js to get UTC time as date object instead of string

I want to get the UTC time using moment.utc() as a date object instead of epoch or string (moment.utc().format() or using .toISOString()). moment.utc().toDate() returns my local time. Any help would be appreciated.

Upvotes: 3

Views: 5714

Answers (1)

RobG
RobG

Reputation: 147363

You can use moment().toDate(). If you coerce the date to a string (e.g. by sending it to the console, alert, etc.), the built-in (implementation dependent) toString method will typically use the host timezone settings to generate a string, e.g.

var m = moment().toDate();  // Equivalent to new Date()

console.log(m + '') // coerce to string uses built-in toString
console.log(m.toISOString()) // ISO 8601 string offset +0000
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

Upvotes: 1

Related Questions