1). javascript 获取当前时间:

var now = new Date();  //  Wed Dec 14 2016 18:22:12 GMT+0800 (中国标准时间)

2). javascript 时间转化为时间戳:

var timestamp = Date.parse(new Date());  //  1481710793000
var timestamp = (new Date()).valueOf();  //  1481710890340
var timestamp=new Date().getTime();  //  1481710918902

第一种:获取的时间戳是把毫秒改成 000 显示,第二种和第三种是获取了当前毫秒的时间戳。

3). 时间戳转换为时间:

function timeStampToTime(timestamp){ // 时间戳转为时间
    var date = new Date(timestamp);
    Y = date.getFullYear() + '-';
    M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    D = date.getDate() + ' ';
    h = date.getHours() + ':';
    m = date.getMinutes() + ':';
    s = date.getSeconds();
    var time = Y+M+D+h+m+s;
    return time;
}
timeStampToTime(1481710918902); //  2016-12-14 18:21:58

4). 计算前 n 分钟时间:

function desendMinutes(date,minutes)
{
    minutes=parseInt(minutes);
    var interTimes=minutes*60*1000;
    interTimes=parseInt(interTimes);
    return new Date(Date.parse(date)-interTimes);
}
desendMinutes(timeStampToTime(1481710918902),10); // Wed Dec 14 2016 18:11:58 GMT+0800 (中国标准时间)

5). 计算后 n 分钟时间:

function addMinutes(date,minutes)  
{
    minutes=parseInt(minutes);
    var interTimes=minutes*60*1000;
    interTimes=parseInt(interTimes);
    return new Date(Date.parse(date)+interTimes);
}
addMinutes(timeStampToTime(1481710918902),10); // Wed Dec 14 2016 18:31:58 GMT+0800 (中国标准时间)

6). 自定义时间格式:

Date.prototype.format = function(format) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(), //day
        "H+": this.getHours(), //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
        "S": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format))
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(format))
            format = format.replace(RegExp.$1, RegExp.$1.length == 1
                ? o[k]
                : ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}
​
new Date().format('yy年MM月dd日HH时mm分ss秒');  //"17年05月11日13时46分52秒"
new Date(1453094034000).format('yyyy-MM-dd HH:mm');  //"2016-01-18 13:13"

7). 自定义时间格式:

Date.prototype.Format = function(formatStr) {
    var str = formatStr;
    var Week = ['日','一','二','三','四','五','六'];
​
    str=str.replace(/yyyy|YYYY/,this.getFullYear());
    str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100));
​
    str=str.replace(/MM/,this.getMonth()>9?(this.getMonth().toString()+1):'0' + (this.getMonth()+1));
    str=str.replace(/M/g,this.getMonth());
​
    str=str.replace(/w|W/g,Week[this.getDay()]);
​
    str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate());
    str=str.replace(/d|D/g,this.getDate());
​
    str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours());
    str=str.replace(/h|H/g,this.getHours());
    str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes());
    str=str.replace(/m/g,this.getMinutes());
​
    str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds());
    str=str.replace(/s|S/g,this.getSeconds());
​
    return str;
};
​
new Date().Format('yy年MM月dd日HH时mm分ss秒  星期W');  //""17年05月18日15时28分47秒  星期四""