relentless-coder
relentless-coder

Reputation: 1536

Momentjs returning date that is two days behind the actual date

I am trying to parse the following date string 22-04-2017. Here is my code

var date = moment.parseZone($scope.booking.startDate, 'DD-MM-YYYY').format();

$scope.booking.startDate is 22-04-2017

But I am getting the following date 2017-04-20T22:13:00-20:17. I don't care for the time, I just need the date to be correct.

How do I fix it?

Upvotes: 0

Views: 202

Answers (1)

VincenzoC
VincenzoC

Reputation: 31482

Simply use moment(String, String) instead of parseZone since your input does not contains time zone information.

As the parseZone docs says:

Moment normally interprets input times as local times (or UTC times if moment.utc() is used). However, often the input string itself contains time zone information. #parseZone parses the time and then sets the zone according to the input string.

Here a working sample:

// Mocking input value
var $scope = {
  booking: {
    startDate: '22-04-2017'
  }
};
var date = moment($scope.booking.startDate, 'DD-MM-YYYY').format();
console.log(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

Upvotes: 1

Related Questions