JS将时间秒转换成天小时分钟秒的字符串

所属分类: 网络编程 / JavaScript 阅读数: 727
收藏 0 赞 0 分享

项目中需求是这样,接口返回的数据中时间单位为秒,但前端显示的时候需要更人性化的带有单位(天,小时,分钟,秒)的字符串;

转换函数如下:

/**
 * 格式化秒
 * @param int  value 总秒数
 * @return string result 格式化后的字符串
 */
function formatSeconds(value) { 
 var theTime = parseInt(value);// 需要转换的时间秒 
 var theTime1 = 0;// 分 
 var theTime2 = 0;// 小时 
 var theTime3 = 0;// 天
 if(theTime > 60) { 
  theTime1 = parseInt(theTime/60); 
  theTime = parseInt(theTime%60); 
  if(theTime1 > 60) { 
   theTime2 = parseInt(theTime1/60); 
   theTime1 = parseInt(theTime1%60); 
   if(theTime2 > 24){
    //大于24小时
    theTime3 = parseInt(theTime2/24);
    theTime2 = parseInt(theTime2%24);
   }
  } 
 } 
 var result = '';
 if(theTime > 0){
  result = ""+parseInt(theTime)+"秒";
 }
 if(theTime1 > 0) { 
  result = ""+parseInt(theTime1)+"分"+result; 
 } 
 if(theTime2 > 0) { 
  result = ""+parseInt(theTime2)+"小时"+result; 
 } 
 if(theTime3 > 0) { 
  result = ""+parseInt(theTime3)+"天"+result; 
 }
 return result; 
} 

ps:下面看下js时间戳与时间日期间相互转换

今天在工作中要将获取到的时间转换为时间戳,一时间竟不知道怎么用,于是不得不去查询资料,这里特地做个笔记。

  1、将日期转换为时间戳。

  要将日期转换为时间戳,首先得先获取到日期,这里可以直接指定日期,或者是使用当前日期。要获取当前日期,我们可以使用new Date()来获取。直接上代码。

// (1)、将当前日期转换为时间戳。
 var now = new Date();
 console.log(now.getTime()) // 将当前日期转换为时间戳,getTime()方法可返回距1970年1月1日之间的毫秒数。也可以使用 +now ,该效果等同于now.getTime()

// (2)、将指定日期转换为时间戳。
 var t = "2017-12-08 20:5:30"; // 月、日、时、分、秒如果不满两位数可不带0.
 var T = new Date(t); // 将指定日期转换为标准日期格式。Fri Dec 08 2017 20:05:30 GMT+0800 (中国标准时间)
 console.log(T.getTime()) // 将转换后的标准日期转换为时间戳。

 2、将时间戳转换为日期。

var t = 787986456465; // 当参数为数字的时候,那么这个参数就是时间戳,被视为毫秒,创建一个距离1970年1月一日指定毫秒的时间日期对象。
console.log(new Date(t)) // Wed Dec 21 1994 13:07:36 GMT+0800 (中国标准时间)

var t2 = "2017-5-8 12:50:30";
console.log(new Date(t2)) // Mon May 08 2017 12:50:30 GMT+0800 (中国标准时间)

var t3 = "2017-10-1";
console.log(new Date(t3)) // Sun Oct 01 2017 00:00:00 GMT+0800 (中国标准时间) 不设定时分秒,则默认转换为00:00:00

  将时间戳转换为指定格式日期的方法封装:

// 格式化日期,如月、日、时、分、秒保证为2位数
function formatNumber (n) {
 n = n.toString()
 return n[1] ? n : '0' + n;
}
// 参数number为毫秒时间戳,format为需要转换成的日期格式
function formatTime (number, format) {
 let time = new Date(number)
 let newArr = []
 let formatArr = ['Y', 'M', 'D', 'h', 'm', 's']
 newArr.push(time.getFullYear())
 newArr.push(formatNumber(time.getMonth() + 1))
 newArr.push(formatNumber(time.getDate()))

 newArr.push(formatNumber(time.getHours()))
 newArr.push(formatNumber(time.getMinutes()))
 newArr.push(formatNumber(time.getSeconds()))

 for (let i in newArr) {
  format = format.replace(formatArr[i], newArr[i])
 }
 return format;
}

  如需要调用上述方法,使用formatTime(1545903266795, 'Y年M月D日 h:m:s')或者formatTime(1545903266795, 'Y-M-D h:m:s')即可

以上所述是小编给大家介绍的JS将时间秒转换成天小时分钟秒的字符串,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

更多精彩内容其他人还在看

jQuery LigerUI 使用教程表格篇(1)

ligerGrid是ligerui系列插件的核心控件,用户可以快速地创建一个美观,而且功能强大的表格,支持排序、分页、多表头、固定列等等
收藏 0 赞 0 分享

JavaScript中常用的运算符小结

JavaScript中常用的运算符小结,需要的朋友可以参考下。
收藏 0 赞 0 分享

深入理解JavaScript系列(13) This? Yes,this!

在这篇文章里,我们将讨论跟执行上下文直接相关的更多细节。讨论的主题就是this关键字。实践证明,这个主题很难,在不同执行上下文中this的确定经常会发生问题
收藏 0 赞 0 分享

javascript (用setTimeout而非setInterval)

javascript (用setTimeout而非setInterval)如果用setInterval 可能出现 下次调用会在前一次调用前调用
收藏 0 赞 0 分享

JavaScript中两个感叹号的作用说明

用两个感叹号的作用就在于,如果明确设置了o中flag的值(非null/undefined/0""/等值),自然test就会取跟o.flag一样的值;如果没有设置,test就会默认为false,而不是null或undefined
收藏 0 赞 0 分享

javascript写的简单的计算器,内容很多,方法实用,推荐

最近用javascript写了一个简单的计算器,自己测试感觉还好,代码都给了注释,非常不错,推荐大家学习。
收藏 0 赞 0 分享

js的表单操作 简单计算器

javascript写的简单的加减乘除计算器,里面涉及到一些方法还是很实用的哦,新手不要错过
收藏 0 赞 0 分享

Jquery中删除元素的实现代码

empty用来删除指定元素的子元素,remove用来删除元素,或者设定细化条件执行删除
收藏 0 赞 0 分享

javaScript 利用闭包模拟对象的私有属性

JavaScript缺少块级作用域,没有private修饰符,但它具有函数作用域。作用域的好处是内部函数可以访问它们的外部函数的参数和变量(除了this和argument
收藏 0 赞 0 分享

为JavaScript类型增加方法的实现代码(增加功能)

大家在js开发过程中有些功能已经满足不了我们的需求,或没有我们需要的功能,那么我们就可以自己扩展下,个性化js
收藏 0 赞 0 分享
查看更多