最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How to multiply time using javascript? - Stack Overflow

programmeradmin0浏览0评论

I have the following timespan ing 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 have the following timespan ing 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

Share Improve this question asked Apr 30, 2015 at 11:46 DarkShadowAYDarkShadowAY 1754 silver badges15 bronze badges 1
  • 4 I'd say get the total milliseconds from the TimeSpan, multiply that in your JS and convert it to whatever granularity you need. – Andrei V Commented Apr 30, 2015 at 11:49
Add a ment  | 

2 Answers 2

Reset to default 5

I am bining 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 unment 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

发布评论

评论列表(0)

  1. 暂无评论