站长资源网络编程

jQuery时间戳和日期相互转换操作示例

整理:jimmy2026/7/30浏览2
简介本文实例讲述了jQuery时间戳和日期相互转换操作。分享给大家供大家参考,具体如下:网上找的很多都没都是这样显示的2017-8-7 3:5:3 自己搜索改下了一下加了0这样显示 2017-08-07 15:05:03(function($) , /** * 日期 转换为 Unix时间戳 * @param <string> 2014-01-01 20:20:20 日期格式 * @return <int> unix时间戳(秒) */ DateToUnix: function(string) { var f = string.split(' ', 2); var d = (f[0] ""; ymdhis += time.getUTCFullYear() + "-"; ymdhis += ((time.getUTCMonth()+1) < 10 "0" + (time.getUTCMonth()+1) : (time.getUTCMonth()+1)) + "-"; ymdhis += (time.getUTCDate() < 10 "0" + time.getUTCDate() : time.getUTCDate()) + " "; ymdhis += (time.getHours() < 10 "0" + time.getHours() : time.getHours()) + ":"; ymdhis += (time.getUTCMinutes() < 10 "0" + time.getUTCMinutes() : time.getUTCMinutes()) + ":"; ymdhis += (time.getUTCSeconds() < 10 "0" + time.getUTCSeconds() : time.getUTCSeconds()); if (isFull === true) { ymdhis += (time.getHours() < 10 "0" + time.getHours() : time.getHours()) + ":"; ymdhis += (time.getUTCMinutes() < 10 "0" + time.getUTCMinutes() : time.getUTCMinutes()) + ":"; ymdhis += (time.getUTCSeconds() < 10 "0" + time.getUTCSeconds() : time.getUTCSeconds()); } return ymdhis; } } }); })(jQuery);

调用方法:

<script>
  document.write($.myTime.DateToUnix('2017-08-07 10:49:59')+'<br>');
  document.write($.myTime.UnixToDate(1502085303));
</script>

使用在线HTML/CSS/JavaScript代码运行工具:http://tools.jb51.net/code/HtmlJsRun,测试得到如下运行结果:

jQuery时间戳和日期相互转换操作示例

PS:这里再为大家推荐几款时间及日期相关工具供大家参考使用:

在线日期/天数计算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi

在线日期计算器/相差天数计算器:
http://tools.jb51.net/jisuanqi/datecalc

在线日期天数差计算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq

Unix时间戳(timestamp)转换工具:
http://tools.jb51.net/code/unixtime

更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jQuery日期与时间操作技巧总结》、《jQuery扩展技巧总结》、《jQuery常见事件用法与技巧总结》、《jQuery常用插件及用法总结》、《jQuery常见经典特效汇总》及《jquery选择器用法总结》

希望本文所述对大家jQuery程序设计有所帮助。

上一篇:JS实现的小火箭发射动画效果示例

下一篇:ES6的Fetch异步请求的实现方法