把时间分成几分钟和几秒钟

时间:2015-04-30 11:20:53

标签: javascript

我有以下代码,应该做我需要的:

function fromSeconds(seconds, showHours = false) {
    if(showHours) {
        var hours = Math.floor(seconds / 3600),
            seconds = seconds - hours * 3600;
    }
    var minutes = (Math.floor(seconds/60) < 10) ? 
        "0" + Math.floor(seconds/60) : Math.floor(seconds/60);
    var seconds = (seconds % 60 > 9) ? seconds % 60 : "0" + seconds % 60;

    if(showHours) {
        var timestring = hours + ":" + minutes + ":" + seconds;
    } else {
        var timestring = minutes + ":" + seconds;
    }
    return timestring;
}

问题在于我也有:

var video = $('#home_explainer_placeholder');
video.bind("timeupdate", function() {
    $('#currentTime').html(video[0].currentTime.toFixed(2));
    $('#remTime').html((video[0].duration - video[0].currentTime).toFixed(2));
    $('#totalTime').html(video[0].duration.toFixed(2));
});

而且我不知道如何应用第一个代码,以便例如currentTime显示如下:minutes:seconds

请帮忙吗?

4 个答案:

答案 0 :(得分:1)

您可以将video[0].currentTime之类的值传递给函数fromSeconds,该函数将返回格式化字符串

var video = $('#home_explainer_placeholder');
video.bind("timeupdate", function () {
    $('#currentTime').html(fromSeconds(video[0].currentTime));
    $('#remTime').html(fromSeconds(video[0].duration - video[0].currentTime));
    $('#totalTime').html(fromSeconds(video[0].duration));
});

答案 1 :(得分:1)

通过小固定,您可以将其保留为:

Demo

authorize

答案 2 :(得分:0)

 $('#currentTime').html(function(){
     var time=video[0].currentTime.toFixed(2);
     //some conversion needed in-order to convert to required format
     fromSeconds(time,false)//returns time 
 });

答案 3 :(得分:0)

假设- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker { NSLog(@"the post title is :%@ %@",marker.userData,marker.title); CustomInfoWindow *view = [[[NSBundle mainBundle] loadNibNamed:@"CustomInfoWindow" owner:self options:nil] objectAtIndex:0]; // Your Created Custom View XIB. UILabel *theLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 50,150, 50)];// Create a Custom Label and add it on the custom view. theLabel.text = marker.title; // marker.title is the title of pin. [view addSubview:theLabel]; return view; } 是以秒为单位的时间,您需要将值传递给您的函数。

currentTime会返回您需要的文字,因此fromSeconds会根据需要返回fromSeconds(mytimevalue)

mm:ss

另一种选择是使用JavaScript的video.bind("timeupdate", function() { $('#currentTime').html( fromSeconds(video[0].currentTime) ); $('#remTime').html( fromSeconds(video[0].duration - video[0].currentTime) ); $('#totalTime').html( fromSeconds(video[0].duration) ); }); 对象,它以毫秒为值: Date()

然后,您可以使用var currentTime = new Date(video[0].currentTime * 1000);Date.getMinutes()来查找您的值。

更多详情here