栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

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

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

本文实例讲述了jQuery时间戳和日期相互转换操作。分享给大家供大家参考,具体如下:

网上找的很多都没都是这样显示的2017-8-7 3:5:3 自己搜索改下了一下加了0这样显示 2017-08-07 15:05:03

(function($) {
    $.extend({
      myTime: {
 
 CurTime: function(){
   return Date.parse(new Date())/1000;
 },
 
 DateToUnix: function(string) {
   var f = string.split(' ', 2);
   var d = (f[0] ? f[0] : '').split('-', 3);
   var t = (f[1] ? f[1] : '').split(':', 3);
   return (new Date(
parseInt(d[0], 10) || null,
(parseInt(d[1], 10) || 1) - 1,
parseInt(d[2], 10) || null,
parseInt(t[0], 10) || null,
parseInt(t[1], 10) || null,
parseInt(t[2], 10) || null
     )).getTime() / 1000;
 },
 
 UnixToDate: function(unixTime, isFull, timeZone) {
   if (typeof (timeZone) == 'number')
   {
     unixTime = parseInt(unixTime) + parseInt(timeZone) * 60 * 60;
   }
   var time = new Date(unixTime * 1000);
   var ymdhis = "";
   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);

调用方法:



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

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程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/77941.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号