search
HomeWeb Front-endFront-end Q&AWhat is javascript timestamp?
What is javascript timestamp?Dec 06, 2021 pm 04:19 PM
javascriptTimestamp

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

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
}
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

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;);

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(&#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;   
    }   
}

[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!

Statement
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
es6数组怎么去掉重复并且重新排序es6数组怎么去掉重复并且重新排序May 05, 2022 pm 07:08 PM

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

JavaScript的Symbol类型、隐藏属性及全局注册表详解JavaScript的Symbol类型、隐藏属性及全局注册表详解Jun 02, 2022 am 11:50 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

原来利用纯CSS也能实现文字轮播与图片轮播!原来利用纯CSS也能实现文字轮播与图片轮播!Jun 10, 2022 pm 01:00 PM

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

JavaScript对象的构造函数和new操作符(实例详解)JavaScript对象的构造函数和new操作符(实例详解)May 10, 2022 pm 06:16 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

JavaScript面向对象详细解析之属性描述符JavaScript面向对象详细解析之属性描述符May 27, 2022 pm 05:29 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

javascript怎么移除元素点击事件javascript怎么移除元素点击事件Apr 11, 2022 pm 04:51 PM

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

整理总结JavaScript常见的BOM操作整理总结JavaScript常见的BOM操作Jun 01, 2022 am 11:43 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach是es6里的吗foreach是es6里的吗May 05, 2022 pm 05:59 PM

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft