码迷,mamicode.com
首页 > Web开发 > 详细

前端(js/jquery) 日期和时间戳的转换

时间:2015-12-30 11:23:02      阅读:6732      评论:0      收藏:0      [点我收藏+]

标签:

一、JavaScript中获取当前时间的时间戳

  方法一:

var timestamp=Date.parse(new Date());   ====》结果是:1451441086000

  :这种方式精确到秒,毫秒位置上的用0代替了。

  方法二:

var timestamp=(new Date()).valueOf();  ====》结果是:1451441232779

 注:这两种方法获取从 1970年1月1日午夜开始的毫秒数

  方法三:

  javascript 中使用 new Date().getTime() 方法IE8 以上版本可以使用 直接使用Date.now()方法

//IE8以上版本
  if (!Date.now) {
      Date.now = function() { return new Date().getTime(); };
  }  
jQuery 获取时间戳 $.now() var timestamp = $.now();

 

二、jquery中将具体时间转换成时间戳

  1、引入jquery.extend.date.js插件

<script type="text/javascript" src="../js/jquery.extend.date.js"></script>

  2、代码原理

(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) + "-";
                ymdhis += time.getUTCDate();
                if (isFull === true)
                {
                    ymdhis += " " + time.getUTCHours() + ":";
                    ymdhis += time.getUTCMinutes() + ":";
                    ymdhis += time.getUTCSeconds();
                }
                return ymdhis;
            }
        }
    });
})(jQuery);

  3、应用

$.mytime.DateToUnix(‘2015-12-30 12:30:15‘); //日期转换为时间戳

$.mytime.UnixToDate(1451442143254);  //时间戳转换为日期

前端(js/jquery) 日期和时间戳的转换

标签:

原文地址:http://www.cnblogs.com/renxiaoren/p/5087972.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!