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


    一、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);  //时间戳转换为日期
  • 相关阅读:
    JavaScript中循环的使用方法
    认识了解JavaScript
    div的对齐显示
    四宫格div
    使用div写一个计算机
    div基本操作
    CSS样式
    Electron的逆向
    Electron输出:Hello,World
    Electron的安装-Windows
  • 原文地址:https://www.cnblogs.com/renxiaoren/p/5087972.html
Copyright © 2020-2023  润新知