资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

怎么在jQuery中对时间戳和日期进行转换

怎么在jQuery中对时间戳和日期进行转换?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

目前创新互联公司已为上1000家的企业提供了网站建设、域名、网络空间、绵阳服务器托管、企业网站设计、昌江黎族网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

具体如下:

(function($) {
    $.extend({
      myTime: {
        /**
         * 当前时间戳
         * @return   unix时间戳(秒)
         */
        CurTime: function(){
          return Date.parse(new Date())/1000;
        },
        /**
         * 日期 转换为 Unix时间戳
         * @param  2014-01-01 20:20:20 日期格式
         * @return   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  unixTime 待时间戳(秒)
         * @param  isFull 返回完整时间(Y-m-d 或者 Y-m-d H:i:s)
         * @param  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);

调用方法:

关于怎么在jQuery中对时间戳和日期进行转换问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


本文名称:怎么在jQuery中对时间戳和日期进行转换
文章网址:http://cdkjz.cn/article/jpgsej.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220