Home>Article>Web Front-end> What is javascript timestamp?
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.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
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.
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
function formatDate(date) { var y = date.getFullYear(); var m = date.getMonth() + 1; m = m < 10 ? '0' + m : m; var d = date.getDate(); d = d < 10 ? ('0' + d) : d; return y + '-' + m + '-' + d;//这里可以写格式 //输出:2018-03-24 }
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() + ' '; var h = date.getHours() + ':'; var m = date.getMinutes() + ':'; var s = date.getSeconds(); return Y+M+D+h+m+s; } timestampToTime(1403058804); console.log(timestampToTime(1403058804));//2014-06-18 10:33:24
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('date1大'); } else { console.log('date2大'); } } compareDate('2018-10-27','2018-10-28');
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") )
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('-'); var sdate=new Date(arys1[0],parseInt(arys1[1]-1),arys1[2]); arys2=checkEndDate.split('-'); 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; } }
[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!