Home > Web Front-end > Front-end Q&A > What is javascript timestamp?

What is javascript timestamp?

青灯夜游
Release: 2021-12-06 16:19:15
Original
5130 people have browsed it

In JavaScript, timestamp refers to the total number of seconds from January 1, 1970 00:00:00 Greenwich Time (midnight UTC/GMT) to the present. A timestamp is usually a sequence of characters that uniquely identifies a certain moment in time.

What is javascript timestamp?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

What is the timestamp?

The timestamp refers to the time from 00:00:00 on January 1, 1970, Greenwich Time (midnight of UTC/GMT, that is, January 1, 08, 1970, Beijing time The total number of seconds since (hour 00 minutes 00 seconds) to now.

A timestamp is usually a sequence of characters that uniquely identifies a certain moment in time.

Convert date to timestamp

var date = new Date('2014-04-23 18:55:49:123');
// 有三种方式获取
// 精确到毫秒
var time1 = date.getTime();
console.log(time1);//1398250549123
// 精确到毫秒
var time2 = date.valueOf();
console.log(time2);//1398250549123
// 只能精确到秒,毫秒用000替代
var time3 = Date.parse(date);
console.log(time3);//1398250549000
Copy after login

Convert timestamp to date

function formatDate(date) {
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? &#39;0&#39; + m : m;
    var d = date.getDate();
    d = d < 10 ? (&#39;0&#39; + d) : d;    return y + &#39;-&#39; + m + &#39;-&#39; + d;//这里可以写格式
    //输出:2018-03-24
}
Copy after login
function timestampToTime(timestamp) {
    var date = new Date(timestamp * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + &#39;-&#39;;
    var M = (date.getMonth()+1 < 10 ? &#39;0&#39;+(date.getMonth()+1) : date.getMonth()+1) + &#39;-&#39;;
    var D = date.getDate() + &#39; &#39;;
    var h = date.getHours() + &#39;:&#39;;
    var m = date.getMinutes() + &#39;:&#39;;
    var s = date.getSeconds();    return Y+M+D+h+m+s;
}
timestampToTime(1403058804);
console.log(timestampToTime(1403058804));//2014-06-18 10:33:24
Copy after login

Time stamp function

1. Compare the two dates individually

function compareDate(date1,date2){
    var oDate1 = new Date(date1);
    var oDate2 = new Date(date2);    
    if(oDate1.getTime() > oDate2.getTime()){
        console.log(&#39;date1大&#39;);
    } else {
        console.log(&#39;date2大&#39;);
    }
}
compareDate(&#39;2018-10-27&#39;,&#39;2018-10-28&#39;);
Copy after login

2. Compare the 24 hours of the day individually

function compareTime(t1,t2)  {  
    var date = new Date();  
    var a = t1.split(":");  
    var b = t2.split(":");  
    return date.setHours(a[0],a[1]) > date.setHours(b[0],b[1]);  
}  
console.log( compareTime("12:00","11:15") )
Copy after login

3. Compare date plus time

//比较日期,时间大小  
function compareCalendar(startDate, endDate) {   
    if (startDate.indexOf(" ") != -1 && endDate.indexOf(" ") != -1 ) {   
        //包含时间,日期  
        compareTime(startDate, endDate);               
    } else {   
        //不包含时间,只包含日期  
        compareDate(startDate, endDate);   
    }   
} 
function compareDate(checkStartDate, checkEndDate) {      
    var arys1= new Array();      
    var arys2= new Array();      
    if(checkStartDate != null && checkEndDate != null) {      
        arys1=checkStartDate.split(&#39;-&#39;);      
        var sdate=new Date(arys1[0],parseInt(arys1[1]-1),arys1[2]);      
        arys2=checkEndDate.split(&#39;-&#39;);      
        var edate=new Date(arys2[0],parseInt(arys2[1]-1),arys2[2]);      
        if(sdate > edate) {      
            alert("日期开始时间大于结束时间");         
            return false;         
        }  else {   
            alert("通过");   
            return true;      
        }   
    }      
} 
function compareTime(startDate, endDate) {   
    if (startDate.length > 0 && endDate.length > 0) {   
        var startDateTemp = startDate.split(" ");   
        var endDateTemp = endDate.split(" ");   
        var arrStartDate = startDateTemp[0].split("-");   
        var arrEndDate = endDateTemp[0].split("-");   
        var arrStartTime = startDateTemp[1].split(":");   
        var arrEndTime = endDateTemp[1].split(":");   
        var allStartDate = new Date(arrStartDate[0], arrStartDate[1], arrStartDate[2], arrStartTime[0], arrStartTime[1], arrStartTime[2]); 
        var allEndDate = new Date(arrEndDate[0], arrEndDate[1], arrEndDate[2], arrEndTime[0], arrEndTime[1], arrEndTime[2]);   
        if (allStartDate.getTime() >= allEndDate.getTime()) {   
            alert("startTime不能大于endTime,不能通过");   
            return false;   
        } else {   
            alert("startTime小于endTime,所以通过了");   
            return true;   
        }   
    } else {   
        alert("时间不能为空");   
        return false;   
    }   
}
Copy after login

[Related recommendations: javascript learning tutorial]

The above is the detailed content of What is javascript timestamp?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template