js毫秒数转换为具体日期

时间:2024-02-19 14:50:57

【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