I am implementing a function to have a countdown in Angular form current time - existing time in future. If the time has elapsed then display a message. Timer ran out in ..... HH:MM:SS
The end time. Lets call it endTime eg:
9/15/2016 9:16:00 PM
Current time. Time current moment we live.
Lets call it currentTime.
The goal is to get a timer that is Current time - end time. Save it to a Variable TotalHours.
Then calculate the time remaining for NOW to total hours. For example TotalHours = 5. And NOW is 9/14/2016 1:16:00 PM then FinalCountDown = 6:16:00 PM. That is the timer I want running...
Here is how I am doing it...
if (info.endTime) {
var CurrentTime = new Date().toLocaleString('en-US');
moment.locale(); // en
var TotalHours = moment.utc(moment(info.diffTime, "DD/MM/YYYY HH:mm:ss").diff(moment(CurrentTime, "DD/MM/YYYY HH:mm:ss"))).format("HH:mm:ss");
info.finalCountDown= TotalHours;
};
The issue here is the following:
Case 1:
endTime = 9/15/2016 9:16:00 AM
currentTime = 9/15/2016 1:21:00 PM
TotalHours = 4:05:00
But... if its after next 2 days...
Case 2:
endTime = 9/17/2016 9:16:00 AM
currentTime = 9/15/2016 1:21:00 PM
TotalHours = 4:05:00
Total hours is still the same...
I need it to add 24hours + 24 hours + extra time = 48 + 4:05:00 = 52:05:00
also I want it to display as: 52h:05m:00s
Please let me know how to solve this...
A quick and dirty solution would be to simply convert the difference between the two date/time objects to milliseconds and then do some math on the milliseconds and format the output as follows:
var currentTime = new Date("9-15-2016 13:21:00");
var endTime = new Date("9-17-2016 09:16:00");
var ms = (endTime - currentTime); // ms of difference
var days = Math.round(ms/ 86400000);
var hrs = Math.round((ms% 86400000) / 3600000);
var mins = Math.round(((ms% 86400000) % 3600000) / 60000);
$scope.finalCountdown = (days + "d:" + hrs + " h:" + mins + "m left");
You could add in a calculation for the seconds if you needed and you can do some formatting of the numbers to have leading zeros.
However, doing this doesn't account for issues such as leap-years and other data and time anomalies. A better suggestion would be to use angular-moment which utilizes Moment.js as it can handle differences and formatting with ease.
Related
There is start date and end date. Using moment ,will get difference between 2 dates in hours.
var now = moment(sessionData.StartTime);
var end = moment(sessionData.EndTime);
var duration = moment.duration(end.diff(now));
var days = duration.asHours();
it returns : 3.08 .
I want to show that difference like this- 3 hrs 15 m.
Is this possible to achieve
Try get the difference between two days using diff followed by expressing it terms of duration. Then you would be able to get exact number of days, hours, minutes
let now = moment("2017-01-26T14:21:22+0000");
let expiration = moment("2017-01-29T17:24:22+0000");
let diff = expiration.diff(now);
let diffDuration = moment.duration(diff);
let dayDiff = diffDuration.days() + "d";
let hoursDiff = diffDuration.hours() + "hrs";
let minDiff = diffDuration.minutes() + "m";
console.log(`${dayDiff} ${hoursDiff} ${minDiff}`);
I want to count how many hours and minutes between two timestamp which are generated by Date.parse. After I get the difference, I need to convert it into hours and minutes like 2.10 (means, 2 hours and 10 minutes). Once I have read that to do it you need to divide it with 3600 so I tried this code but it just gives me 0.89 instead of 1.26.
var now = new Date();
var endTime = Date.parse(now)/1000;
var startTime = Date.parse("2018-03-16 10:29:17")/1000;
$scope.timestamp_difference = startTime - endTime;
$scope.hours = $scope.timestamp_difference/3600;
How to do it right?
In case you haven't heard, Momentjs makes working with dates and times pretty damn easy in javascript i updated code for you may hope it will helps you now
var date1 = moment('03/15/2018 11:00', 'MM/DD/YYYY hh:mm'),
date2 = moment('03/16/2018 10:00', 'MM/DD/YYYY hh:mm');
var duration = moment.duration(date2.diff(date1));
//you will get 23 hours 00 minute
alert(duration.asHours().toFixed(2))
http://jsfiddle.net/dp7rzmw5/9771/
Output: 23:00 hrs
function getTimeDifference(timestampDifference) {
var hours = ~~(timestampDifference / 3600);
var minuts = ~~((timestampDifference - hours * 3600) / 60);
var seconds = timestampDifference - hours * 3600 - minuts * 60;
return hours + '.' + minuts + '.' + seconds;
}
var minutes = "0" + Math.floor(timestampDifference / 60);
// get minutes
var seconds = "0" + Math.floor(timestampDifference - minutes * 60);
// get seconds
var hour = "0" + Math.floor(timestampDifference / 3600);
//get hour
if ( hour > 1 ) {
return hour.substr(-2) + ":" + minutes.substr(-2) + ":" + seconds.substr(-2);
} else {
return minutes.substr(-2) + ":" + seconds.substr(-2);
}
// check if the hour is greater then 1 than return time with hour else return minutes.
I need to book a vehicle.So i get date.BUt i didnt got the time.Actually i want to book at least 2 hours after current time.also i need to get full time from next day onwards please help me.Iam doing in Ionic framework and AngularJS
Thanks in Advance
var timeNow = new Date().getTime();
var twoHoursIntoFuture = new Date(time + 1000 * 60 * 60 * 2);
the new Date() creates a new date object from current time. getTime() gets the time in milliseconds since January 1st, 1970. Then we just add 2 hours into timeNow (1000 * 60 * 60 * 2 milliseconds) and make a new date object based on that.
Check http://www.w3schools.com/jsref/jsref_obj_date.asp for more date manipulation info.
this.myActualDate = new Date(new Date().getTime() + (3 * 24 * 60 * 60 * 1000)).toISOString().toString().substring(0, 10);
this.myMaxDate = new Date(new Date().getTime() + (368 * 24 * 60 * 60 * 1000)).toISOString();
<ion-datetime displayFormat="YYYY/MM/DD" pickerFormat="YYYY MMMM DDDD" min={{myActualDate}} max={{myMaxDate}} [(ngModel)]="myActualDate"
formControlName="booking_date" class="flex-date" required></ion-datetime>
You can modify this code to perform yours.
If you want to extend three days in actual date then you can edit 365 digit into 368.
It will add three days in your date.
I think it will also work for time.
If you edit hours , minutes and seconds.
I'm using the Jquery TimeCircles plug in (https://github.com/wimbarelds/TimeCircles) as a timer on my application, but I would like to have it so that there is only one circle with the minutes and seconds in the middle, like 15:40. I would like the seconds to keep ticking down, but the circle should animate according to the minutes only. Currently I have two circles showing the minutes and seconds.
I would like to start the timer at 50 minutes, and then countdown to 0 minutes and 0 seconds. Is there any way I can have the time display in the format MM:SS inside the one circle, and have the number of seconds ticking down, and the circle animating to the number of minutes ticking down only?
Thank you so much!
I actually had a fairly similar request the other day on github:
https://github.com/wimbarelds/TimeCircles/issues/68
You could change it to something like:
var $container = $('#DateCountdown .textDiv_Minutes');
$container.find('h4').text('Time left');
var $original = $container.find('span');
var $clone = $original.clone().appendTo($container);
$original.hide();
$('#DateCountdown').TimeCircles().addListener(function(unit, value, total) {
total = Math.abs(total);
var minutes = Math.floor(total / 60) % 60;
var seconds = total % 60;
if(seconds < 10) seconds = "0" + seconds;
$clone.text(minutes + ':' + seconds);
}, "all");
You'd need to use the TimeCircles options that make it only display the Minutes circle.
I want to display time duration of a video in silverlight 3.0 application. The format should be like 00:20 / 05:00 . ( Current Video time / Total Video Time). How to get it??.
TimeSpan _duration = mediaElement.NaturalDuration.TimeSpan;
var totalTime = string.Format("{0:00}:{1:00}", (_duration.Hours * 60) + _duration.Minutes, _duration.Seconds);
TimeSpan remaining = _duration.Subtract(mediaElement.Position);
var currentTime = string.Format("{0:00}:{1:00}", (remaining.Hours * 60) + remaining.Minutes, remaining.Seconds);
string result = string.formar("{0}/{1}",totalTime,currentTime);
Try this, Hope this can help.
Regards.