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


Posted in Javascript onJuly 10, 2019

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

转换函数如下:

/**
 * 格式化秒
 * @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将时间秒转换成天小时分钟秒的字符串,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

Javascript 相关文章推荐
jQuery autocomplate 自扩展插件、自动完成示例代码
Mar 28 Javascript
jquery利用ajax调用后台方法实例
Aug 23 Javascript
点击弹出层效果&弹出窗口后网页背景变暗效果的实现代码
Feb 10 Javascript
php,js,css字符串截取的办法集锦
Sep 26 Javascript
原生js和jquery实现图片轮播特效
Apr 23 Javascript
jQuery Ztree行政地区树状展示(点击加载)
Nov 09 Javascript
jQuery Ajax全解析
Feb 13 Javascript
详解通过源码解析Node.js中cluster模块的主要功能实现
May 16 Javascript
jQuery实现的自定义轮播图功能详解
Dec 28 jQuery
如何在Angular应用中创建包含组件方法示例
Mar 23 Javascript
深入浅析Vue 中 ref 的使用
Apr 29 Javascript
vue 项目软键盘回车触发搜索事件
Sep 09 Javascript
js Array.slice的8种不同用法示例
Jul 10 #Javascript
vue-router二级导航切换路由及高亮显示的实现方法
Jul 10 #Javascript
Vue编程式跳转的实例代码详解
Jul 10 #Javascript
微信小程序在ios下Echarts图表不能滑动的问题解决
Jul 10 #Javascript
Vue事件修饰符native、self示例详解
Jul 09 #Javascript
如何自定义微信小程序tabbar上边框的颜色
Jul 09 #Javascript
微信小程序wx.request拦截器使用详解
Jul 09 #Javascript
You might like
php中实现进程锁与多进程的方法
2016/09/18 PHP
PHP如何将图片文件上传到另外一台服务器上
2019/08/26 PHP
基于Jquery实现表格动态分页实现代码
2011/06/21 Javascript
jQuery EasyUI API 中文文档 - PropertyGrid属性表格
2011/11/18 Javascript
jquery js 获取时间差、时间格式具体代码
2013/06/05 Javascript
jquery选择符快速提取web表单数据示例
2014/03/27 Javascript
JavaScript必知必会(六) delete in instanceof
2016/06/08 Javascript
jQuery插件扩展测试实例
2016/06/21 Javascript
javascript实现的全国省市县无刷新多级关联菜单效果代码
2016/08/01 Javascript
js实现String.Fomat的实例代码
2016/09/02 Javascript
基于JS+Canves实现点击按钮水波纹效果
2016/09/15 Javascript
JS实现动态增加和删除li标签行的实例代码
2016/10/16 Javascript
react中的ajax封装实例详解
2017/10/17 Javascript
webpack4+express+mongodb+vue实现增删改查的示例
2018/11/08 Javascript
vue使用代理解决请求跨域问题详解
2019/07/24 Javascript
详解解决小程序中webview页面多层history返回问题
2019/08/20 Javascript
[01:00:04]DOTA2上海特级锦标赛B组小组赛#1 Alliance VS Spirit第二局
2016/02/26 DOTA
[54:19]完美世界DOTA2联赛PWL S2 Magma vs PXG 第二场 11.28
2020/12/01 DOTA
Python with用法实例
2015/04/14 Python
Python的Flask开发框架简单上手笔记
2015/11/16 Python
利用python程序帮大家清理windows垃圾
2017/01/15 Python
对Python3 goto 语句的使用方法详解
2019/02/16 Python
详解pandas的外部数据导入与常用方法
2019/05/01 Python
PyTorch的SoftMax交叉熵损失和梯度用法
2020/01/15 Python
python实现简单颜色识别程序
2020/02/19 Python
CSS3支持IE6, 7, and 8的边框border属性
2012/12/28 HTML / CSS
css3和jquery实现自定义checkbox和radiobox组件
2014/04/22 HTML / CSS
西班牙三叶草药房:Farmacias Trébol
2019/05/03 全球购物
Ticketmaster意大利:音乐会、节日、艺术和剧院的官方门票
2019/12/23 全球购物
初三化学教学反思
2014/01/23 职场文书
家长对小学生的评语
2014/01/28 职场文书
彩色的非洲教学反思
2014/02/18 职场文书
机关会计岗位职责
2014/04/08 职场文书
质量月口号
2014/06/20 职场文书
2019年市场部个人述职报告(三篇)
2019/10/23 职场文书
使用Python拟合函数曲线
2022/04/14 Python