Hasan Al-Natour
Hasan Al-Natour

Reputation: 2066

momentjs subtract return the same value

i am parsing a date from a time value looks like 1256 formats to HHmm :

let parsedTime = moment(time, "HHmm");
console.log(parsedTime)
//moment("2017-11-07T12:56:00.000") 


console.log(parsedTime.subtract(3, 'hours'))
//moment("2017-11-07T12:56:00.000")

what am i doing wrong ?

Upvotes: 1

Views: 934

Answers (2)

Niladri
Niladri

Reputation: 5962

if you observe the _d property from the returned object in the console, it holds the modified value after the subtraction .

let time = '1256';
let parsedTime = moment(time, "HHmm");
console.log(parsedTime)
/* Object 
 { _isAMomentObject: true, _i: "1256", _f: "HHmm", _isUTC: false, _pf: Object, _locale: Object, _d: Date 2017-11-08T07:26:00.000Z, _isValid: true } */


console.log(parsedTime.subtract(3, 'hours'));

/* Object { _isAMomentObject: true, _i: "1256", _f: "HHmm", _isUTC: false, _pf: Object, _locale: Object, _d: Date 2017-11-08T04:26:00.000Z, _isValid: true } */

Upvotes: 1

Namaskar
Namaskar

Reputation: 2119

I'm guessing you were observing the moment object which has a property _i which tells you the initial value. The actual value can be seen with format

let time = moment("1256", "HHmm");
console.log(time.format())
//moment("2017-11-07T12:56:00.000")


console.log(time.subtract(3, 'hours').format())
//moment("2017-11-07T9:56:00.000")
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.min.js"></script>

Upvotes: 1

Related Questions