D.Dev
D.Dev

Reputation: 15

Get calculated date from moment object react

I have react a component that need to add some hours to a given date according to the local date.

render() {
let offset = new Date().getTimezoneOffset();
let localDate1=(moment(processItem.beginTime).add(-offset/60,'h'));
return(
     <p>{localDate1}</p>/*need to display the 2019-09-18T16:02:00.00*/
 )
}

Here processItem.beginTime format is '2019-09-18T21:49:40+08:00'.When I console log the 'localDate1' it.gives the following:

{ 
    _isAMomentObject: true, 
    _i: "2019-09-18T18:32:00+08:00", 
    _f: "YYYY-MM-DDTHH:mm:ssZ",
    _tzm: 480, 
    _isUTC: false, 
    _pf: {…}, 
    _locale: {…}, 
    _d: Date 2019-09-18T16:02:00.000Z, 
    _isValid: true 
}

The correct value is '_d: Date 2019-09-18T16:02:00.000Z' and I want to display it in my code. How can I extract that value from the object ?

Upvotes: 1

Views: 371

Answers (2)

D.Dev
D.Dev

Reputation: 15

Here I got the value which I wanted to display by

let localDate1=(moment(processItem.beginTime).add(-offset/60,'h').toJSON());
return(
 <p>{localDate1}</p> /*display the 2019-09-18T16:02:00.00*/
)

.toJSON() solved my problem here

Upvotes: 0

Dupocas
Dupocas

Reputation: 21307

According to Moment's docs

const offset = moment(new Date()).utcOffset()
const str = moment(processItem.beginTime).add(offset, 'minutes')
const result = moment(str).format()//iso yyyy-MM-ddThh:mm:ss
console.log(result)

Upvotes: 1

Related Questions