Convert HH:MM:SS into minute using javascript - javascript

How can I Convert HH:MM:SS into minute using javascript ?

Use split and multiply per 60 ignoring seconds.
Taking this answer as base:
var hms = '02:04:33'; // your input string
var a = hms.split(':'); // split it at the colons
// Hours are worth 60 minutes.
var minutes = (+a[0]) * 60 + (+a[1]);
console.log(minutes);
Use split and multiply per 60 using seconds as decimal (thus is not full exact)
var hms = '02:04:33'; // your input string
var a = hms.split(':'); // split it at the colons
// Hours are worth 60 minutes.
var minutes = (+a[0]) * 60 + (+a[1]);
console.log(minutes + "," + ((+a[2]) / 60));

To know time functions & formats in JS you must read the manual.
var date = new Date();
console.log(date.getMinutes());

Related

returning the number of days between 2 dates is not working

I have a small code that picks up the dates from a json file.
and returns the amount of days left before it expires.
How ever its returning NaN in console log.
var start = "2019/03/12";
var end = "2020/03/12";
days = (end- start) / (1000 * 60 * 60 * 24);
console.log(Math.round(days));
this should be correct. but its not working.
You need to change end and start to Date
var start = "2019/03/12";
var end = "2020/03/12";
days = ( new Date(end)- new Date(start) ) / (1000 * 60 * 60 * 24);
console.log(Math.round(days));
Try this...using Date objects
var start = new Date("2019/03/12");
var end = new Date("2020/03/12");
days = (end - start) / (1000 * 60 * 60 * 24);
console.log(Math.round(days));
you have to convert your string date into a javascript date but overall i would recommend to use moment as javascript dates can be a pain
to convert your string into a javascript datetype you can do it like this
var mydate = new Date('2011-04-11T10:20:30Z'); // <--- you have to format it
approach 2)
new Date('2011', '04' - 1, '11', '11', '51', '00')
if you want to use moment, you can do it like this:
var mydate = moment("2014-02-27T10:00:00").format('DD-MM-YYYY'); // <-- here inside the format function you can define how your string get's parsed
Use diff function of moment.js. But you have to format it before use.
const format = date => date.replace(/\//g, '-')
var start = moment(format("2019/03/12"));
var end = moment(format("2020/03/12"));
console.log(end.diff(start, 'days'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

Converting hours,minutes into seconds by parsing youtube t= queryparam in javascript/JQuery

I want to parse the timestamp t= in youtube url("http://youtu.be/O4tzHn-EuHc?t=1h5m16s") and calculate the total number of seconds that i need to pass as a start parameter for embed youtube url "http://www.youtube.com/embed/XGSy3_Czz8k?start="
To get the hours, minutes and seconds i use reg-exp's as below. Let me know any improvement can be done in code to make it simple.
var url ="http://youtu.be/XGSy3_Czz8k?t=1h5m16s";
var timeStamp = url.match("t=(.)*?[&|(\s)]");
var hours = timeStamp[0].match(/(\d)+h/);
var minutes = timeStamp[0].match(/(\d)+m/);
var seconds = timeStamp[0].match(/(\d)+s/);
var totalTimeInSeconds = 0;
if (hours) {
hours = hours[0].replace("h","");
totalTimeInSeconds += hours * 60 * 60;
}
if (minutes) {
minutes = minutes[0].replace("m","");
totalTimeInSeconds += minutes * 60;
}
if (seconds) {
seconds = seconds[0].replace("s","")
totalTimeInSeconds += seconds * 1;
}
console.log("hours:"+hours);
console.log("minutes:"+minutes);
console.log("seconds:"+seconds);
console.log("TotalTimeInSeconds:"+ totalTimeInSeconds);
<iframe width="420" height="345"
src="http://www.youtube.com/embed/XGSy3_Czz8k?start="+totalTimeInSeconds>
</iframe>
I think a good source for getting comments on your code would be codereview.
You can get rid of your String.replace calls by slightly adjusting your regexes to read like this:
var hours = timeStamp[0].match(/(\d+)h/);
var minutes = timeStamp[0].match(/(\d+)m/);
var seconds = timeStamp[0].match(/(\d+)s/);
With these regexes you will capture all digits at once, and can than use them like this:
if (hours) {
totalTimeInSeconds += parseInt(hours[1], 10) * 60 * 60;
}
if (minutes) {
totalTimeInSeconds += minutes[1] * 60;
}
if (seconds) {
totalTimeInSeconds += seconds[1];
}
The use of parseInt is not necessary there,
but I'd probably introduce it to make it more explicit that conversion is taking place. I'd also suggest adjusting the regex for your timeStamp variable so that it already narrows down on the t parameter more.
I think the easiest is to use RegExp replace function:
var seconds = "1h5m16s".replace(/([0-9]+)h([0-9]+)m([0-9]+)s/, function(match, p1, p2 ,p3) {
return p1 * 60 * 60 + p2 * 60 + p3 * 1
})
Note p3 * 1 - it is a shortcut for parseInt. Also note that replace will return you a string - don't forget to convert to a number if needed.
Try this
var url ="http://youtu.be/XGSy3_Czz8k?t=1h5m16s";
var timeStamp = url.match("t=(.)*?[&|(\s)]");
timeStampSplitted = timeStamp[0].replace("t=","").replace("h", ":").replace("m", ":").replace("s", "").split(':');
// minutes are worth 60 seconds. Hours are worth 60 minutes.
var seconds = (+timeStampSplitted[0]) * 60 * 60 + (+timeStampSplitted[1]) * 60 + (+timeStampSplitted[2]);

How to multiply time using javascript?

I have the following timespan coming from a model in MVC:
timeTaken = "00:01:00";
Then I have a multiplier
multiply = "3";
Result: 00:03:00
What would be the best way to calculate this time?
I don't know a great deal of libraries. I was thinking of splitting the seconds, minutes and hours, dividing each one into seconds, multiplying then putting it back together.
However, I have this kind of calculations for many sections, it just seems a little mundane. Can I just multiply the time in a better manner?
Thanks
I am combining the snippets I found in multiple pages. Conversion of hh:mm:ss to seconds, multiply 3x and then again convert to hh:mm:ss.
var hms = '00:01:00'; // your input string
var a = hms.split(':'); // split it at the colons
// minutes are worth 60 seconds. Hours are worth 60 minutes.
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);
var newSeconds= 3*seconds;
// multiply by 1000 because Date() requires miliseconds
var date = new Date(newSeconds * 1000);
var hh = date.getUTCHours();
var mm = date.getUTCMinutes();
var ss = date.getSeconds();
// If you were building a timestamp instead of a duration, you would uncomment the following line to get 12-hour (not 24) time
// if (hh > 12) {hh = hh % 12;}
// These lines ensure you have two-digits
if (hh < 10) {hh = "0"+hh;}
if (mm < 10) {mm = "0"+mm;}
if (ss < 10) {ss = "0"+ss;}
// This formats your string to HH:MM:SS
var t = hh+":"+mm+":"+ss;
document.write(t);
JSFiddle
First you can convert them to seconds as below
var hms = "00:01:00";
var a = hms.split(':'); // split it at the colons
// minutes are worth 60 seconds. Hours are worth 60 minutes.
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);
var newSeconds=seconds * 3;
var t = new Date();
t.setSeconds(newSeconds);
console.log(t);
DEMO
Update
To just obtain time do as below
var time=t.toTimeString().split(' ')[0]
DEMO
UPDATE
To obtain just hour from time you can do as follows
t.toTimeString().split(' ')[0].split(':')[0]
and to obtain hour in 12 hour format you can do as below:
var hour;
if(t.toTimeString().split(' ')[0].split(':')[0]>12)
hour=t.toTimeString().split(' ')[0].split(':')[0]-12;
else
hour=t.toTimeString().split(' ')[0].split(':')[0];
alert(hour);
UPDATED DEMO

convert seconds to {H hr MM min} time format with javascript

How can I convert seconds to (H hr mm min) format by Javascript?
Example : 4 hr 30 min
I found other solutions here, but they didn't help me.
hours is
(total_seconds / 60) / 60
minutes is
(total_seconds / 60) % 60
seconds is
(total_seconds % 60) % 60
where / is integer division (division that discards the remainder) and % is the modulo function.
Use JavaScript's built-in Date function:
// Randomly selected number of seconds
var seconds = 23568;
// Pass it to the Date-constructor (year, month, day, hours, minutes, seconds)
var d = new Date(0, 0, 0, 0, 0, seconds);
// Get result as a "formatted" string, and show it.
var myString = d.getHours().toString() + ':' + d.getMinutes().toString() + ':' + d.getSeconds().toString();
alert(myString);
Below is the given code which will convert seconds into hh-mm-ss format:
var measuredTime = new Date(null);
measuredTime.setSeconds(4995); // specify value of SECONDS
var MHSTime = measuredTime.toISOString().substr(11, 8);
Source: Convert seconds to HH-MM-SS format in JavaScript

Hour to Minutes 00:00 to 00 javascript

I need to convert hours:minutes (00:00) to minutes 00 in Javascript.
I thought about doing it by using substr to get hour and minutes separately and then multiply the hours part by 60 and then add the minutes part.
Is there any other easy way to do this?
It's pretty easy with split:
var str = "04:17";
var parts = str.split(":");
var minutes = parseInt(parts[0], 10) * 60 + parseInt(parts[1], 10);
console.log(minutes); // 257 (four hours and seventeen minutes)
To split in hour and minute, you can use the split() function on the String object:
"12:05".split(':');
--> ["12", "05"]
Then you need to convert the Strings in the array to integers with parseInt:
var hours = parseInt("12", 10);
var minutes = parseInt("05", 10);
Then rest is simple calculation:
var total = hours * 60 + minutes;

Categories

Resources