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;
Related
I am currently working on Jvascript datetime part in that getting NaN error while converting hours and minutes to seconds like strtotime in PHP so I want to know how to convert minutes and seconds like the way we do in strtotime in PHP.
var d = new Date();
var total = d.getHours() + ":" + d.getMinutes();
var ts = Date.parse(total);
document.write(ts);
In output getting error NaN
This is a sort of inane question, but here's the number of seconds in the hours and minutes of that number:
var d = new Date();
var total = (d.getHours() * 60 * 60) + (d.getMinutes() * 60);
document.write(total);
First of all, Date.parse() takes a string of a specific format (such as Jul 18, 2018). Second, it will not convert the date to seconds, but will return the number of milliseconds since January 1, 1970 00:00:00 GMT.
If you need to convert hh:mm to seconds, the correct approach is to multiply the value of getHours() by 3600 and multiply the value of getMinutes() by 60, then sum up the two values.
var d = new Date();
var timeinsecs = d.getHours() * 3600 + d.getMinutes() * 60;
document.write(timeinsecs);
While if you need to get the time in seconds from January 1, 1970 00:00:00 GMT till the current time, you will need to parse the current date then divide by 1000:
var d = new Date();
document.write(Date.parse(d) / 1000);
Just get hours and minutes, then sum them multiplying hours * 3600 and minutes * 60, like this
var d = new Date();
var total = d.getHours() * 3600 + d.getMinutes() * 60;
document.write(total)
If you want to follow your original approach of not doing the math by hand, you need to include a date before the time (any date should do, could be today if you wish) and convert ms to seconds (both of these for the reasons Wais Kamal pointed out) as follows.
var d = new Date();
var total = d.getHours() + ":" + d.getMinutes();
var someDate ='July 4, 1776';//works, but maybe safer to choose since 1990
total=someDate+', '+total;
var ts = Date.parse(total);
document.write((ts- Date.parse(someDate))/1000);
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());
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
This is how I had it done:
var sDate = $('.start input').datepicker('getDate').getTime();
var nDate = $('.end input').datepicker('getDate').getTime();
var dias = Math.floor((nDate - sDate)/1000/60/60/24) + 1;
But it fails
20/03/2014 to 30/03/2014 -> 11 days
and
21/03/2014 to 31/03/2014 -> 10 days, when the difference is the same,
Where is the flaw?
The right code is this (as #vinod-gubbala stated above):
var dias = Math.round((nDate - sDate)/(1000*60*60*24));
Basically, you get the difference in (milliseconds) of the days and divide them by 1000 (to concert to seconds) * 60 (60 seconds per minute) * 60 (60 minutes per hour) * 24 (24 hours a day).
Don't know why you are adding +1 at the end. Of course this will work with complete days, I mean, comparing dates with he same time.
The problem you are experiencing could be something with the daylight saving time. Have in mind that for 2014, the last sunday of march (march 30th) there is a time change (at least in Europe), so there is an hour less and your function, as it do a floor, rounds down and you lose a day.
Regards.
You have to round instead of floor.
var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var sDate = $('.start input').datepicker('getDate').getTime();
var nDate = $('.end input').datepicker('getDate').getTime();
var diffDays = Math.round(Math.abs((nDate - sDate)/(oneDay)));
Take a look at http://momentjs.com/
Your code will look like:
var a = moment($('.start input').datepicker('getDate').getTime());
var b = moment($('.end input').datepicker('getDate').getTime());
d = a.diff(b, 'days')
I did this:
var d1 = new Date('2013-03-20 00:00:00')
var d2 = new Date('2013-03-30 00:00:00')
(d2.getTime() - d1.getTime()) / 1000 / 60 / 60 / 24 + 1; //11
And then this:
var d1 = new Date('2013-03-21 00:00:00')
var d2 = new Date('2013-03-31 00:00:00')
(d2.getTime() - d1.getTime()) / 1000 / 60 / 60 / 24 + 1; //11
So there is no flaw there, most likely it's an error in creation of Date() object of the jQuery datepicker. I suggest you do the following:
console.log(nDate,sDate);
console.log(((nDate - sDate)/1000/60/60/24)+1);
And see what does that give you for both dates. You might spot an error there.
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