1

How can I find the time difference between two time pickers in MaterializeCSS, while also having a space before AM/PM and momentJS? I want a space before AM/PM because that will allow me to use it internationally.

Example:

Timepicker 1 = 8:00 AM Timepicker 2 = 5:00 PM

Find: Timepicker 2 - Timepicker 1 in HH:mm format

We tried without spaces and it failed. We then tried with spaces and it worked, but we cannot change the format property of the MaterializeCSS timepicker component. We are not using the 'HH:mm:ss' format (24 hour format). We want to use the 'HH:mm t' formart

Here is our Attempt:

    $('#tTimeEntryStarted, #tTimeEntryEnd').pickatime({
        default: '00:00', // Set default time: 'now', '1:30AM', '16:30'
        fromnow: 0,       // set default time to * milliseconds from now (using with default = 'now')
        twelvehour: true, // Use AM/PM or 24-hour format
        donetext: '', // text for done-button
        //cleartext: 'Clear', // text for clear-button
        min: undefined,
        max: undefined,
        canceltext: '', // Text for cancel-button,
        container: undefined, // ex. 'body' will append picker to body 
        autoclose: true, // automatic close timepicker
        ampmclickable: true, // make AM PM clickable
        aftershow: function () { } //Function for after opening timepicker
    });

Following works as space added:

 console.log(moment( new Date(moment(new Date()).format('MM/DD/YYYY') + " " + $("#tTimeEntryEnd").val().replace("AM"," AM").replace("PM"," PM")) -  new Date(moment(new Date()).format('MM/DD/YYYY') + " " + $("#tTimeEntryStarted").val().replace("AM"," AM").replace("PM"," PM")), 'HH:mm'))

The following does not work, but we want something like this so that we can use this internationally (ex: replacing "AM" with " AM" will not work in China, as they have something else for AM):

 console.log(moment( new Date(moment(new Date()).format('MM/DD/YYYY') + " " + $("#tTimeEntryEnd").val()) -  new Date(moment(new Date()).format('MM/DD/YYYY') + " " + $("#tTimeEntryStarted").val()), 'HH:mm'))

The following two will not work. as there is no space before AM:

new Date("2018/1/1 " + moment($("#tTimeEntryEnd").val(), 'hh:mm a'))
new Date("2018/1/1 8:30AM")

The following works, because there is a space before AM, but we cannot change the MaterializeCSS time picker format as HH:mm T:

new Date("2018/1/1 8:30 AM")
SajithG
  • 130
  • 4
  • Possible duplicate of https://stackoverflow.com/questions/18623783/get-the-time-difference-between-two-datetimes – Tanmay Jun 28 '18 at 10:47

1 Answers1

0

You can first create a moment with the given times with

moment("8:00 AM", 'hh:mm a')

and same for the other/end time. And then take the difference between them in hours or minutes for example

var startTime = moment("8:00AM", 'hh:mm a'),
     endTime = moment("5:00PM", 'hh:mm: a');
    
 console.log("in hours", endTime.diff(startTime, 'hours'))
 
 console.log("in minutes", endTime.diff(startTime, 'minutes'))
<script src="https://rawgit.com/moment/moment/2.2.1/min/moment.min.js"></script>
Muhammad Usman
  • 10,039
  • 22
  • 39