从毫秒到小时,分钟,秒和毫秒 [英] From milliseconds to hour, minutes, seconds and milliseconds

查看:426
本文介绍了从毫秒到小时,分钟,秒和毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从毫秒去的(小时,分,秒,毫秒),再$ P $一个元组psenting的时间相同。例如:

I need to go from milliseconds to a tuple of (hour, minutes, seconds, milliseconds) representing the same amount of time. E.g.:

10799999ms = 2H59米59S 999ms

10799999ms = 2h 59m 59s 999ms

下面的伪code是我能想出的唯一的事情:

The following pseudo-code is the only thing I could come up with:

# The division operator below returns the result as a rounded down integer
function to_tuple(x):
    h = x / (60*60*1000)
    x = x - h*(60*60*1000)
    m = x / (60*1000)
    x = x - m*(60*1000)
    s = x / 1000
    x = x - s*1000
    return (h,m,s,x)

我敢肯定,它必须是能够做到这一点聪明/更优雅/更快/更紧凑。

I'm sure it must be possible to do it smarter/more elegant/faster/more compact.

推荐答案

下面是我会怎么做它在Java中:

Here is how I would do it in Java:

int seconds = (int) (milliseconds / 1000) % 60 ;
int minutes = (int) ((milliseconds / (1000*60)) % 60);
int hours   = (int) ((milliseconds / (1000*60*60)) % 24);

这篇关于从毫秒到小时,分钟,秒和毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆