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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Nliki/article/details/72983689
     在游戏开发过程中我们常用的时间,我通常写一个专门的类比如: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秒)? 这个有兴趣的可以试下,就会明白。



猜你喜欢

转载自blog.csdn.net/Nliki/article/details/72983689
今日推荐