时间戳转换为yyyy-mm-dd hh:mm:ss格式

1.时间戳转换为yyyy-mm-dd hh:mm:ss格式

function timestampToTime(timestamp) {
        var date = new Date(timestamp * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
        var Y = date.getFullYear() + '-';
        var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1):date.getMonth()+1) + '-';
        var D = (date.getDate()< 10 ? '0'+date.getDate():date.getDate())+ ' ';
        var h = (date.getHours() < 10 ? '0'+date.getHours():date.getHours())+ ':';
        var m = (date.getMinutes() < 10 ? '0'+date.getMinutes():date.getMinutes()) + ':';
        var s = date.getSeconds() < 10 ? '0'+date.getSeconds():date.getSeconds();
        return Y+M+D+h+m+s;
    }
 timestampToTime(1403058804);
 console.log(timestampToTime(1403058804));//2020-06-18 10:33:24

2.当前时间转换为时间戳

// 获取当前时间戳(以s为单位)
var timestamp = Date.parse(new Date());
timestamp = timestamp / 1000;
//当前时间戳为:1403149534
console.log("当前时间戳为:" + timestamp);

3.将yyyy-mm-dd hh:mm:ss格式转换为时间戳

// 获取某个时间格式的时间戳
var stringTime = "2014-07-10 10:21:12";
var timestamp2 = Date.parse(new Date(stringTime));
timestamp2 = timestamp2 / 1000;
//2014-07-10 10:21:12的时间戳为:1404958872 
console.log(stringTime + "的时间戳为:" + timestamp2);

4.将时间戳转换为其他格式

var timestamp3 = 1403058804;
var newDate = new Date();
newDate.setTime(timestamp3 * 1000);
console.log(newDate.toDateString());// Wed Jun 18 2014 
console.log(newDate.toGMTString());// Wed, 18 Jun 2014 02:33:24 GMT 
console.log(newDate.toISOString());// 2014-06-18T02:33:24.000Z
console.log(newDate.toJSON());// 2014-06-18T02:33:24.000Z 
console.log(newDate.toLocaleDateString());// 2014年6月18日 
console.log(newDate.toLocaleString());// 2014年6月18日 上午10:33:24 
console.log(newDate.toLocaleTimeString());// 上午10:33:24 
console.log(newDate.toString());// Wed Jun 18 2014 10:33:24 GMT+0800 (中国标准时间)
console.log(newDate.toTimeString());// 10:33:24 GMT+0800 (中国标准时间) 
console.log(newDate.toUTCString());// Wed, 18 Jun 2014 02:33:24 GMT

5.还有一种是将当前时间可自定义格式

Date.prototype.format = function(format) {
    /*
     * eg:format="YYYY-MM-dd hh:mm:ss";

     */
    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;
}

var startTime = new Date().format("yyyy-MM-dd 00:00:00");
var endTime = new Date().format("yyyy-MM-dd hh:mm:ss");
console.log(startTime)//2021-02-06 00:00:00
console.log(endTime)//2021-02-06 13:30:48

6.js日期对象,获取及设置日期的方法

1)创建日期对象

var date=new Date(); 现在时间
  1. “月/日/年 时:分:秒" “时:分:秒 月/日/年” 字符串
  2. 年,月,日,时,分,秒 不能加""
  3. 注意:不传参的话,会得到当前时间的信息
  4. 设置月份的时候要’-1’获取月份要’+1’

2)获取日期信息的方法

getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31)。		几号
getDay() 从 Date 对象返回一周中的某一天 (0 ~ 6)。			星期
getMonth() 从 Date 对象返回月份 (0 ~ 11)。				月份			
getFullYear() 从 Date 对象以四位数字返回年份。				年份
getYear() 请使用 getFullYear() 方法代替。					
getHours() 返回 Date 对象的小时 (0 ~ 23)。				小时
getMinutes() 返回 Date 对象的分钟 (0 ~ 59)。				分钟
getSeconds() 返回 Date 对象的秒数 (0 ~ 59)。				秒
getMilliseconds() 返回 Date 对象的毫秒(0 ~ 999)。			毫秒			
getTime() 返回 197011 日至今的毫秒数。				格林尼治时间至今的毫秒数
getTimezoneOffset() 返回本地时间与格林威治标准时间 (GMT) 的分钟差

3)设置日期的方法

setDate() 设置 Date 对象中月的某一天 (1 ~ 31)setMonth() 设置 Date 对象中月份 (0 ~ 11)setFullYear() 设置 Date 对象中的年份(四位数字)。
setYear() 请使用 setFullYear() 方法代替。
setHours() 设置 Date 对象中的小时 (0 ~ 23)setMinutes() 设置 Date 对象中的分钟 (0 ~ 59)setSeconds() 设置 Date 对象中的秒钟 (0 ~ 59)setMilliseconds() 设置 Date 对象中的毫秒 (0 ~ 999)setTime() 以毫秒设置 Date 对象。
setUTCDate() 根据世界时设置 Date 对象中月份的一天 (1 ~ 31)setUTCMonth() 根据世界时设置 Date 对象中的月份 (0 ~ 11)setUTCFullYear() 根据世界时设置 Date 对象中的年份(四位数字)。
setUTCHours() 根据世界时设置 Date 对象中的小时 (0 ~ 23)setUTCMinutes() 根据世界时设置 Date 对象中的分钟 (0 ~ 59)setUTCSeconds() 根据世界时设置 Date 对象中的秒钟 (0 ~ 59)setUTCMilliseconds() 根据世界时设置 Date 对象中的毫秒 (0 ~ 999)
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐