时间戳基础知识
时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数,也就是说,每过一秒,当前时间戳加1,所以可以根据这个进行时间的关系运算
php中的时间格式处理
获取当前时间戳
time();
将时间戳转换为标准时间格式
date('Y-m-d H:i:s', 1491447794)
将标准时间格式转换为时间戳
strtotime("2017-04-06 11:03:14")
js中的事件格式处理
js有时间对象,实例化一下,打印出来看看,这个时间对象有很多方法,js对于事件格式的处理都是基于这个对象,以及对象的方法来进行字符串处理。
console.log(new Date());//Thu Apr 06 2017 11:49:48 GMT+0800 (中国标准时间)
获取当前时间戳
 Math.ceil(Date.parse(new Date()) / 1000);// Date转换出来的是毫秒级时间戳,所以要除以1000
时间戳转换日期格式
function getDate(time) {
    var date = new Date(parseInt(time) * 1000).toLocaleString();
    return date;
}
根据时间戳获得年月日 或时分秒
//  一位数前补零
function getTen(num){
    if ( num < 10) {
        return "0" + num;
    }
    return num;
}
// 获取年月日格式
function onlyDate(time) {
    var date = new Date(parseInt(time)*1000);
    var year = date.getFullYear();
    var month = getTen(date.getMonth()+1);
    var day = getTen(date.getDate());
    return year + '-' + month + '-' + day;
}
// 获取时分秒格式
function onlyTime(time){
    var date = new Date(parseInt(time)*1000);
    var hour = getTen(date.getHours());
    var minutes = getTen(date.getMinutes());
    return hour + ':' + minutes;
}
根据时间格式获取时分秒
 // 根据date格式获得时分秒
 function toSecond(date){
    var date = new Date(date);
    var hour = getTen(date.getHours());
    var minutes = getTen(date.getMinutes());
    var second = getTen(date.getSeconds());
    return hour + ':' + minutes + ':' + second;
}    
// 根据date格式获得年月日
function toDate(date){
    var date = new Date(date);
    var year = date.getFullYear();
    var month = getTen(date.getMonth()+1);
    var day = getTen(date.getDate());
    return year + '-' + month + '-' + day;
}