当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jQuery时间戳和日期相互转换操作示例

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

2019年01月08日  | 移动技术网IT编程  | 我要评论
本文实例讲述了jquery时间戳和日期相互转换操作。分享给大家供大家参考,具体如下: 网上找的很多都没都是这样显示的2017-8-7 3:5:3 自己搜索改下了一下加了0

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

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

(function($) {
    $.extend({
      mytime: {
        /**
         * 当前时间戳
         * @return <int>  unix时间戳(秒)
         */
        curtime: function(){
          return date.parse(new date())/1000;
        },
        /**
         * 日期 转换为 unix时间戳
         * @param <string> 2014-01-01 20:20:20 日期格式
         * @return <int>  unix时间戳(秒)
         */
        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;
        },
        /**
         * 时间戳转换日期
         * @param <int> unixtime 待时间戳(秒)
         * @param <bool> isfull 返回完整时间(y-m-d 或者 y-m-d h:i:s)
         * @param <int> timezone 时区
         */
        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);

调用方法:

<script>
  document.write($.mytime.datetounix('2017-08-07 10:49:59')+'<br>');
  document.write($.mytime.unixtodate(1502085303));
</script>

使用在线html/css/javascript代码运行工具:,测试得到如下运行结果:

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

在线日期/天数计算器:

在线日期计算器/相差天数计算器:

在线日期天数差计算器:

unix时间戳(timestamp)转换工具:

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

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网