0

I'm working with jQuery time entry plugin http://keith-wood.name/timeentry.html
This plugin is returning data in "12:23PM" javascript typeof "string", and now using momentjs I want to convert it into time and get difference between current time & user selected time.

var a = new Date($("#totime").val())

I'm getting error Invalid Date.

Gautam Savaliya
  • 1,403
  • 2
  • 20
  • 31
  • http://stackoverflow.com/questions/18623783/get-the-time-difference-between-two-datetimes –  May 13 '16 at 09:04

1 Answers1

2

You need to use string-format moment constructor to pass string and format in which input string is. To calculate the difference method can be used.

Use

var t = "12:23PM";
var cdt = moment(t, 'HH:mmA');
console.log(cdt.toDate());
console.log('difference in milliseconds: ', cdt.diff(moment()));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.js"></script>
Jed Fox
  • 2,979
  • 5
  • 28
  • 38
Satpal
  • 132,252
  • 13
  • 159
  • 168
  • The difference is in milliseconds.But I need in hrs:min format –  May 13 '16 at 07:22
  • @Varanasi, Yes, I have clearly mentioned __difference in milliseconds:__ You can pass `a.diff(b, 'days')` – Satpal May 13 '16 at 07:24
  • var _a = $scope.Booking.FromTime; var _b = $scope.Booking.ToTime; var _a2 = moment(_a, "HH:mmA") var _b2 = moment(_b, "HH:mmA"); var c = _a2.diff(_b2, 'days'); $log.warn(c); –  May 13 '16 at 07:31
  • I'm working with angularjs, in above code what I have given, is giving me NaN. _a carries current time, _b carries user given time. Im trying to calculate difference. –  May 13 '16 at 07:38