zoukankan      html  css  js  c++  java
  • js毫秒数转换为具体日期

    【1】、毫秒数转换为具体日期

    function getMyDate(str) {
        var oDate = new Date(str),
        oYear = oDate.getFullYear(),
        oMonth = oDate.getMonth()+1,
        oDay = oDate.getDate(),
        oHour = oDate.getHours(),
        oMin = oDate.getMinutes(),
        oSen = oDate.getSeconds(),
        oTime = oYear +'-'+ addZero(oMonth) +'-'+ addZero(oDay) +' '+ addZero(oHour) +':'+
    addZero(oMin) +':'+addZero(oSen);
        return oTime;
    }

    //补零操作
    function addZero(num){
        if(parseInt(num) < 10){
            num = '0'+num;
        }
        return num;
    }

    接口返回的毫秒数如果为string,需要转化为int

    var dateTime = getMyDate(parseInt(data));


     data:  1537444800000
     dateTime:2018-09-20 20:00:00


    【2】、日期转换为毫秒数

    var secondsTime = new Date(dateTime).getTime();


     dateTime:2018-09-20 20:00:00

    secondsTime:  1537444800000

    参考文章:https://blog.csdn.net/bangrenzhuce/article/details/53022894
    ---------------------
    原文:https://blog.csdn.net/tanqiaoxing/article/details/79865989

    重写prototype 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    Date.prototype.Format = function(fmt) {
    var o = {
    "M+" : this.getMonth()+1, //月份
    "d+" : this.getDate(), //日
    "h+" : this.getHours(), //小时
    "m+" : this.getMinutes(), //分
    "s+" : this.getSeconds(), //秒
    "q+" : Math.floor((this.getMonth()+3)/3), //季度
    "S" : this.getMilliseconds() //毫秒
    };
    if(/(y+)/.test(fmt))
    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    for(var k in o)
    if(new RegExp("("+ k +")").test(fmt))
    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
    return fmt;
    }
     
    var today = new Date();
    $('#bdate').val(today.Format("yyyy-MM-dd"));
    $('#edate').val(today.Format("yyyy-MM-dd"));

     截图如下

    从后台获取的为毫秒数,

    首先,将毫秒数转换为Date对象

    第二,将Date对象转换成字符串。也可以使用toLocalString()方法,但是格式难以自定义

    https://www.cnblogs.com/yoxiniao/p/6733268.html

    js 毫秒转换为标准时间

    1
    2
    3
    4
    5
    6
    7
    function dateForm(time){
                    var unixTimestamp = new Date( 1477386005*1000 );
                    commonTime = unixTimestamp.toLocaleString();
                }
                Date.prototype.toLocaleString = function() {
                    return this.getFullYear() + "/" + (this.getMonth() + 1) + "/" + this.getDate() + "/" + this.getHours() + ":" + this.getMinutes() + ":" + this.getSeconds();
                };

      new Data(‘毫秒数’)是标准时间,

      new Data().getTime()是把标准时间转换成毫秒数

    https://www.cnblogs.com/mmzuo-798/p/7591087.html

  • 相关阅读:
    根据IP获取省市 .
    支付宝接口使用步骤及总结
    最新调用优酷视频 免前置广告的方法
    SQL新增数据取表主键最新值
    JS获取地址栏参数
    图片(img标签)的onerror事件
    prototype.js的Ajax对IE8兼容问题解决方案
    基于.net技术的 Rss 订阅开发
    JS获取Dropdownlist选中值
    阿里云tomcat启动慢
  • 原文地址:https://www.cnblogs.com/xiaoshen666/p/10752128.html
Copyright © 2011-2022 走看看