typescript /javascript 中 将秒转化成时分秒

来源:互联网 发布:怎么装潢淘宝店铺 编辑:程序博客网 时间:2024/04/29 08:30
     在游戏开发过程中我们常用的时间,我通常写一个专门的类比如:TimeManager一个管理时间的类,统一管理游戏中的时间以及各式显示等。由于刚开始有点不了解typescript语言,不知道将秒怎么转化成时分秒的,所以就写了这个,将服务器发来的秒转成时分秒。代码如下:


 public static GetTimeLeft2BySecond(s:number): string {      let hours = Math.round((s - 30 * 60) / (60 * 60));      let minutes = Math.round((s - 30) / 60) % 60;      let seconds = s % 60;      return (hours > 0 ? (hours + "小时") : "") + (minutes > 0 ? (minutes + "分钟")  : "") + (seconds > 0 ? (seconds +"秒") : ""); }


效果:


看到上面的代码,有的人会疑惑:小时为什么要减去30*60(半个小时),分钟为什么减去30(30秒)? 这个有兴趣的可以试下,就会明白。



原创粉丝点击