This question already has answers here:
calculate time difference
(4 answers)
Closed 9 years ago.
How to calculate differences between two time eg(server time=04:30 pm and <p id="orderTime">02:30 pm</p>) and return it in minutes such as 90 minutes using jquery and javascript. The server time and order time is in 12 hour format not in 24h.
If you can guarantee they always will be of that format, then here is a straightforward solution:
function humanReadableToMinutes(time)
{
var parts = time.split(/ |:/);
return (parts[2] == 'pm' * 12 * 60)
+ parseInt(parts[0], 10) * 60
+ parseInt(parts[1], 10);
}
http://jsfiddle.net/aYwux/2/
Explanation:
By var parts = time.split(/ |:/); we're splitting 02:30 pm into 3 parts: 02, 30, pm.
return (parts[2] == 'pm' * 12 * 60) + parseInt(parts[0], 10) * 60 + parseInt(parts[1], 10); contains of 3 operands:
* (parts[2] == 'pm' * 12 * 60) --- adds additional 12 hours if it's "pm"
* parseInt(parts[0], 10) * 60 - takes hours fraction extracted and converts it to minutes
* parseInt(parts[1], 10) - minutes fraction
PS: this solution doesn't take care of 12am and 12pm accurately, so it's a homework for the OP
Related
This question already has an answer here:
The correct way to compare time in javascript? [duplicate]
(1 answer)
Closed 1 year ago.
Please, How can I set IF when time is < 21:30 ??
var dt = new Date();
if ((dt.getHours() <= 21) && (dt.getMinutes() <= 30)) { alert("check"); }
This not working when time is example 20:45
You need to check two different things.
if hours <= 20, than everything is true.
if hours == 21, than check minutes.
var dt = new Date('2021/03/18 20:45:00');
if (dt.getHours() <= 20 || (dt.getHours() == 21 && dt.getMinutes() <= 30)) {
alert("check");
}
You could always take the time and convert it to minutes in the day - 1440 (60*24) so 21:30 becomes 21 * 60 + 30 = 1,290.
We can calculate the current minute value by taking the current date time Date.now() (milliseconds since 1/1/1970) mod 86,400,000 (milliseconds in a day) * further divide this by 60,000 (milliseconds in a minute).
(Date.now() % 86_400_000) / (60_000)
It is then trivial to compare these two values
const nineFortyFivePM = 21 * 60 + 30;
const currentMinutes = (Date.now() % 86_400_000) / (60_000);
console.log(`21:45 = ${nineFortyFivePM}`);
console.log(`currentMinutes = ${currentMinutes}`);
if (currentMinutes < nineFortyFivePM)
alert('check');
This question already has answers here:
how to convert minutes to hours using moment.js
(2 answers)
Closed 3 years ago.
I am using moment.js.
I get minutes (max 1440) and i wanted to format that in hours in specific format.
Something like this:
420 minutes is: 07:00
1140 minutes is: 24:00
451 minutes is: 07:31
const time = 1140;
console.log(getTime(time));
function getTime(minutes) {
let hours = parseInt(minutes / 60);
hours = hours < 10 ? `0${hours}` : hours;
let min = minutes % 60;
min = min < 10 ? `0${min}` : min;
return `${hours}:${min}`;
}
This question already has answers here:
JavaScript - Get minutes between two dates
(12 answers)
How can I compare two time strings in the format HH:MM:SS?
(16 answers)
Closed 6 years ago.
I want to display the amount of minutes between the scheduled time and expected time.
This is not to compare, this is to calculate how many minutes there are in different times in both scheduled and expected.
Since both times are displayed as a string, do I need to convert string to a number and then do a comparison?
All I want to return is the difference in time as a number.
Here is my object:
{
station: "Macclesfield",
scheduled: "15:41",
expected: "15:50",
platform: "1"
}
var data = {
station: "Macclesfield",
scheduled: "15:41",
expected: "15:50",
platform: "1"
}
function getTimeDifference(scheduled, expected) {
scheduled = scheduled.split(':'); //get array [hours, minutes]
expected = expected.split(':');
var hours = expected[0] - scheduled[0]; //difference in hours
var minutes = expected[1] - scheduled[1]; //difference in minutes
if (minutes < 0) { //if minutes are negative we know it wasn't a full hour so..
hours--; //subtract an hour
minutes += 60; //add 60 minutes
} //now we're ok
if (hours) //if hours has a value
return hours + ':' + minutes;
return minutes; //hours is 0 so we only need the minutes
}
console.log(getTimeDifference(data.scheduled, data.expected));
data.expected = "16:00";
console.log(getTimeDifference(data.scheduled, data.expected));
data.expected = "17:00";
console.log(getTimeDifference(data.scheduled, data.expected));
var obj = { scheduled: "15:41", expected: "15:50" }
var milliSeconds = Date.parse(`01/01/2011 ${obj.expected}:00`) - Date.parse(`01/01/2011 ${obj.scheduled}:00`)
var minutes = milliSeconds / (1000 * 60)
var hours = milliSeconds / (1000 * 60 * 60)
This question already has answers here:
How to add days to Date?
(56 answers)
Closed 6 years ago.
This is how I get todays timestamp:
var timestampNow = Math.floor(Date.now() / 1000);
I want to set timestampNow to 4 weeks from now.
My initial guess is to use Math.floor(Date.now(28) / 1000);?
I believe this would work:
let fourWeeksFromNow = new Date();
fourWeeksFromNow.setDate(fourWeeksFromNow.getDate() + 28)
javascript isn't that great when it comes to dates, so you need to parse the date to a timestamp and modify the milliseconds. but the maths is not very hard.
var timestamp4weeks = Date.now() + (1000 * 60 * 60 * 24 * 7 * 4)
milliseconds => seconds === * 1000
seconds => minutes === * 60
minutes => hours === * 60
hours => days === * 24
days => weeks === * 7
Moment.js makes this easy. The following are equivalent:
var days = moment().add(28, 'days');
var weeks = moment().add(4, 'weeks');
This question already has answers here:
Convert seconds to HH-MM-SS with JavaScript?
(38 answers)
Closed 6 years ago.
Suppose I have an integer number as a variable. (this number can be any integer number).
Now I want to create a countdown timer based on this number on the page.
To create countDown, I am using jquery-countdownTimer plugin.
A simple usage of this plugin is like this :
$(function(){
$("#hms_timer").countdowntimer({
hours : 3‚
minutes : 10‚
seconds : 10‚
size : "lg"‚
pauseButton : "pauseBtnhms"‚
stopButton : "stopBtnhms"
});
});
As you see , it gets hours , minutes and seconds in 3 separate numbers.
Now my question is how can I convert an integer number to Equivalent hours , minutes and seconds in simplest way?
function getTime(s) {
var secs = parseInt(s, 10); // don't forget the second param
var hours = Math.floor(secs / 3600);
var minutes = Math.floor((secs - (hours * 3600)) / 60);
var seconds = secs - (hours * 3600) - (minutes * 60);
return {
hours: hours,
minutes: minutes,
seconds: seconds
};
}
var time = getTime("3792");
alert("Hours: " + time.hours + "\nMinutes: " + time.minutes + "\nSeconds: " + time.seconds);