How to Perform Timing Subtraction using Moment Js? - momentjs

Iam trying to perform Subtraction in Momentjs:-
// Find the duration between two dates
var breakfast1 = moment('11:32','HH:mm');
var lunch1 = moment('12:52','HH:mm');
alert( moment.duration(lunch1 - breakfast1).humanize() + ' between meals' ); // 1 hours between meals
Output is alerting Only Hours .But How can I get "01:20"as Output Instead of 1 hour Any Ideas ?
how to generate Minutes along with Hours

.humanize() will always round to a specific interval. Break your code up and manually create the display you want...
var breakfast1 = moment('11:32','HH:mm');
var lunch1 = moment('12:52','HH:mm');
var dur = moment.duration(lunch1 - breakfast1);
alert( dur.hours() + ":" + dur.minutes() + ' between meals' );

Related

FullCalendar change date format

When the user select a range of dates on the calendar, a modal opens and the input fields of initial and final dates are auto complete with the dates selected previously. The problem is that they are displaying in this format YYYY-MM-DD and I want it to be DD-MM-YYYY. I have tried everything but nothing seems to work.
Here is where I get the dates and fill the inputs:
select: function (info) {
$('#ModalAdd').modal('show');
$('#ModalAdd').appendTo("body");
$('#activoReservar').val($('#selectActivoReserva option:selected').text());
$('#fechaInicial').val(info.startStr);
var endDate = new Date(info.end);
var beforeDay = new Date(endDate.getFullYear(),endDate.getMonth(),endDate.getDate() - 1).toISOString().slice(0,10);
$('#fechaFinal').val(beforeDay);
},
And here are the things I have tried:
$('#fechaInicial').val(info.startStr.format('ddd, DD-MM-YYYY')); //i tried with dd and a single d too. And without any d
$('#fechaFinal').val(beforeDay.format('ddd, DD-MM-YYYY'));
you may create another function (which you can use anywhere else too),
pass a date to that function and return your desired format.
The sample function might look like as follow:
function dateToDMY(date) {
var d = date.getDate();
var m = date.getMonth() + 1; //Month from 0 to 11
var y = date.getFullYear();
return '' + (d <= 9 ? '0' + d : d) + '-' + (m <= 9 ? '0' + m : m) + '-' + y;
}
And you may call the function from select or any other place as follow:
select: function (selectionInfo) {
var startStr = dateToDMY(selectionInfo.start);
}

How to find difference between two times in 24 hour format - Flutter?

I have to find the difference between two times in 24 hour format. I have the two time strings, Eg: 10:40 and 18:20. How can I find the difference between these two times in Flutter?
You can use intl package.
var format = DateFormat("HH:mm");
var one = format.parse("10:40");
var two = format.parse("18:20");
print("${two.difference(one)}"); // prints 7:40
A complete answer for perfect calculation is given bellow
String start_time = formateTime('12:00'); // or if '24:00'
String end_time = formateTime('24:00'); // or if '12:00
var format = DateFormat("HH:mm");
var start = format.parse(start_time);
var end = format.parse(end_time);
if (start.isAfter(end)) {
print('start is big');
print('difference = ${start.difference(end)}');
} else if(start.isBefore(end)){
print('end is big');
print('difference = ${end.difference(start)}');
}else{
print('difference = ${end.difference(start)}');
}

Moment parse strange behavior with hours

I would like to understand why moment.js is changing minutes to "04" every time I parse, what am I doing wrong?
var sDate = '2017-04-24';
var sHour = '16:54:10';
alert(moment(sDate + ' ' + sHour).format('DD/MM/YYYY HH:MM:SS'));
alert(moment.utc(sDate + 'T' + sHour).format('DD/MM/YYYY HH:MM:SS'));
alert(moment(sDate + ' 00:00:00').format('DD/MM/YYYY HH:MM:SS'));
<script src="https://momentjs.com/downloads/moment-with-locales.min.js">
Accordinly to the official docs it should work.
Moment tokens are case sensitive, please note that uppercase M stands for months while you have to use lowecase m for showing minutes.
See formatting docs.
Note that you probably have to change SS (fractional seconds) to ss (seconds) too.
Your code will be like the following:
var sDate = '2017-04-24';
var sHour = '16:54:10';
alert(moment(sDate + ' ' + sHour).format('DD/MM/YYYY HH:mm:ss'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

.pluck returning undefined in Meteor

Trying to pull a list of ratings from a collection of Reviews and then average them to come up with an aggregated average rating for a Plate. When I look at the data output from the ratings variable I get nothing but "undefined undefined undefined".
averageRating: function() {
var reviews = Reviews.findOne({plateId: this._id});
var ratings = _.pluck(reviews, 'rating');
var sum = ratings.reduce(function(pv, cv){return pv + cv;}, 0);
var avg = sum / ratings.length;
//Testing output
var test = "";
var x;
for (x in reviews) {
text += reviews[x] + ',';
}
return test;
}
Sorry if this is a super newbie question, but I've been at this for hours and cannot figure it out.
I figured out the issue. As listed above var reviews gets set to a cursor which apparently .pluck does not work on. By first converting the cursor to an array of objects I was then able to use .pluck. So updated code looks like this:
averageRating: function() {
var reviewsCursor = Reviews.find({plateId: this._id});
//Converts cursor to an array of objects
var reviews = reviewsCursor.fetch();
var ratings = _.pluck(reviews, 'rating');
var sum = ratings.reduce(function(pv, cv){return pv + cv;}, 0);
var avg = (sum / ratings.length).toPrecision(2);
return avg;
}

Event time on Google analytic

<script type="text/javascript">
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
var timeStamp = (hours + ":" + minutes + " ");</script>
onClick="_gaq.push(['_trackEvent', 'cata', 'act', 'label','timeStamp '])"
I am trying to pass the current time as a value but i cannot get it work
any idea
because the value parameter should be number not string!

Resources