how to calculate time difference between hours in javascript? [duplicate] - javascript

This question already has answers here:
Check time difference in Javascript
(19 answers)
Closed 2 years ago.
i.e. I want to calculate the time difference from 8.30pm to 9pm in 24hr format.
so far i have:
let start = 2030;
let end = 2100;
let timeDiff = end - start;
But this gives me 70 as an answer.
How do I get 30 minutes as an answer?

You need to specify the correct format of your time. you can calculate like below.
let start = new Date('1900/01/01 08:30 PM');
let end = new Date('1900/01/01 09:00 PM');
let minDiff = (Math.abs(start - end) / 1000)/60;
let hourDiff = ((Math.abs(start - end) / 1000)/60)/60;
let dayDiff = parseInt((((Math.abs(start - end) / 1000)/60)/60)/24);

Related

How to calculate is more than one second/hour passed using milliseconds? [duplicate]

This question already has answers here:
Getting difference in seconds from two dates in JavaScript
(2 answers)
Closed 3 years ago.
I want to calculate the time difference between two dates which have different format to know if more than one hour/seconds has passed.
The first format is: 2019-07-02T16:21:00.1030000
The second one is: 1562160899773
I am using getMilliseconds() but I don't know if it's the better way.
for example
var isMoreOneSec = New Date(2019-07-02T16:21:00.1030000).getMilliseconds() - new Date(1562160899773).getMilliseconds() > 1000
getMilliseconds() will return the number of milliseconds in the current second, hence will always return from 0 to 999, the same way getHour() will return from 0 to 23 or getSeconds() from 0 to 59.
When you have two dates, to get the difference in milliseconds you have to use getTime() which will return the number of milliseconds sice unix epoch.
So:
var date1 = new Date("2019-07-02T16:21:00.1030000");
var date2 = new Date(1562160899773);
var date1time = date1.getTime();
var date2time = date2.getTime();
console.log("Date 1 time is", date1time);
console.log("Date 2 time is", date2time);
var dif = date2time - date1time;
console.log("Dif in ms is ", dif);
console.log("Is dif more than one second?", dif > 1000);
console.log("Is dif more than one hour?", dif > 60*60*1000);

Why isn't my IF statement applying to time? [duplicate]

This question already has answers here:
getMinutes() 0-9 - How to display two digit numbers?
(21 answers)
Closed 3 years ago.
I would like to get it to display 0 infront of the output if it's under 10.
const time = new Date();
let hour = today.getHours();
let minute = today.getMinutes();
let second = today.getSeconds();
if (second < 10) {
second = 0 + second;
}
console.log(`Time is ${hour} : ${minute} : ${second}`);
Instead of showing for example 19:5:7, I would like to see 19:05:07
///
Ok, I found out what the problem was. 0 was a number not a string. Just started with JS.
Thanks for the help!
You could pad the value with leading zeroes.
const pad2 = s => s.toString().padStart(2, '0');
let today = new Date;
let hour = pad2(today.getHours());
let minute = pad2(today.getMinutes());
let second = pad2(today.getSeconds());
console.log(`Time is ${hour}:${minute}:${second}`);

How to get the week order number of a Date [duplicate]

This question already has answers here:
Show week number with Javascript?
(15 answers)
Closed 6 years ago.
All:
I wonder if there is a function in javascript that can get the week order of a Date, for example:
01/05/2016 is in the second week of this year, so the week order is 1(let start by 0)
Thanks
You can calculate the days pass from the begining of the year and calculate the number of weeks by deviding it to 7 (yhe number of the days in a week):
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
var week = Math.floor(day/7);
alert(week);

Determine start and end time of current day (UTC) (JAVASCRIPT) [duplicate]

This question already has answers here:
How to get start and end of day in Javascript?
(13 answers)
Closed 7 years ago.
I am trying to determine start and end time of the current day in UTC.
Currently this is my solution. `
var starttime = date.getTime() -(date.getHours() * 3600000 ); //subtracting a hours to go back to start time of the day.
var endtime = low + 86400000; //adding 24 hours to the start time
`
Any help is appreciated ! Thank you
Thank you everyone. This is what worked for me !! :)
var d = new Date();
var year = d.getUTCFullYear();
var month = d.getUTCMonth();
var day = d.getUTCDate();
var startHour =Date.UTC(year,month,day,0,0,0,0);
var endHour = startHour + 86400000;

subtracting military time in javascript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
check time difference in javascript
calculate time difference in javascript
this is my block of code to pull times from a form input,
start = group.find('.startTime').val().replace(':',''), // like 0100
end = group.find('.endTime').val().replace(':',''), // like 0300
timeDiff = (end - start) < 0 ? (end - start + 2400) : (end - start),
timeDiff accounts for times passing midnight, so like if I try and subtract 2300 from 0100, and get -2200, it adds the 2400 to get the correct difference of 0200, or 2 hours.
my problem arises where i try to subtract some times like 2100 - 2030 (which should give me a half hour) but because its just a raw number i get the actual difference of 70. my question is how would I correctly subtract these? If i need to convert it to a date or time object what would be the proper way of doing so? I looked into the setTime method but that didn't sound like what i needed.
Thanks in advance.
Without Date objects (which seems OK for time-only values), you should convert the time to minutes or hours - always calculate with only one unit (and especially never mix them into one decimal number representing a sexagesimal one)!
function toTime(string) {
var m = string.match(/(\d{2}):({\d2})/);
return m && 60 * parseInt(m[1], 10) + parseInt(m[2], 10),
}
var start = toTime( group.find('.startTime').val() ),
end = toTime( group.find('.endTime').val() );
var timeDiff = Math.abs(end - start);
function fromTime(minutes) {
var m = minutes % 60,
h = (minutes - m) / 60;
return ("0"+h).substr(-2)+":"+("0"+m).substr(-2);
}

Categories