shekhardtu
shekhardtu

Reputation: 5370

How to produce date object using moment.js

How to produce a similar new Date() object using moment.js?

I have input as UTC date and output should be similar to new Date() object. Please help to resolve this.

Input: 1389033000000
Output: Tue Jan 07 2014 00:00:00 GMT+0530 (India Standard Time)

Upvotes: 0

Views: 147

Answers (2)

NemoStein
NemoStein

Reputation: 2098

You can use moment().format(pattern), where pattern is a string describing the format you want.

Something like this should work:

const input = 1389033000000
const output = moment(input).format("ddd MMM DD YYYY hh:mm:ss ZZ")

console.log(output)
// Mon Jan 06 2014 04:30:00 GMT-0200

console.log(new Date(input))
// Mon Jan 06 2014 16:30:00 GMT-0200 (Horário de Verão de Brasília)

Furthermore, you can convert a moment to native date with moment().toDate()

const input = 1389033000000
const output = moment(input).toDate()

console.log(output)
// Mon Jan 06 2014 16:30:00 GMT-0200 (Horário de Verão de Brasília)

Upvotes: 1

Şivā SankĂr
Şivā SankĂr

Reputation: 2036

Use Moment Timezone to convert time to particular timezone

var moment = require('moment-timezone');

var timestamp =  1389033000000;
var usingTimezone = moment(timestamp).tz("Asia/Kolkata").format("ddd MMM DD YYYY hh:mm:ss Z");
var usingOffset = moment(timestamp).utcOffset("+05:30").format("ddd MMM DD YYYY hh:mm:ss Z");

console.log(usingTimezone);
console.log(usingOffset);

Upvotes: 0

Related Questions