1. JavasCRIPT time to time stamp
There are five ways to obtain timestamps in JavaScript. The last four methods are to further obtain the current timestamp by instantiating the time object new Date(). JavaScript mainly uses the time object Date to process time.
Method 1: ()
() You can get the current timestamp:
(()) //1642471441587
Method 2: ()
() Convert strings or time objects directly into timestamps:
(new Date()) //1642471535000 ("2022/1/18 10:05") //1642471500000
Note: This method is not recommended, the value at the millisecond level is converted to 000.
Method 3: valueOf()
Obtain the exact timestamp value by returning the original value of the specified object by the valueOf() function:
(new Date()).valueOf() //1642471624512
Method 4: getTime()
Directly obtain the millisecond value of the current time through the prototype method, accurate:
new Date().getTime() //1642471711588
Method 5: Number
Convert a time object into a number type value, i.e. a time stamp
Number(new Date()) //1642471746435
2. JS timestamp to time
We can use the new Date (timestamp) format to convert to obtain the current time, such as:
new Date(1472048779952) Wed Aug 24 2016 22:26:19 GMT+0800 (China Standard Time)
Note: The timestamp parameter must be of Number type. If it is a string, the parsing result is: Invalid Date.
If the backend directly returns the timestamp to the frontend, how can the frontend convert it? The following are two implementation methods
Method 1: Generate '2022/1/18 10:09 AM' format
function getLocalTime(n) { return new Date(parseInt(n)).toLocaleString().replace(/:\d{1,2}$/,' '); } getLocalTime(1642471746435) //'2022/1/18 10:09 am'
You can also use it as follows: If you want to take a few digits, please note that spaces are also counted!
function getLocalTime(n) { return new Date(parseInt(n)).toLocaleString().substr(0,14) } getLocalTime(1642471746435) //'2022/1/18 10 am'
Or use the regular:
function getLocalTime(n){ return new Date(parseInt(n)).toLocaleString().replace(/Year|moon/g, "-").replace(/day/g, " "); } getLocalTime (1642471746435) //'2022/1/18 10:09:06 AM'
Method 2: Generate 'yyyy-MM-dd hh:mm:ss' format
First convert it to a data object, and then use splicing rules and other means to achieve it:
function getData(n){ n=new Date(n) return ().replace(/\//g, "-") + " " + ().substr(0, 8) } getData(1642471746435) //'2022-1-18 10:09:06'
However, this conversion will have an unsatisfactory effect on some browsers, because the toLocaleDateString() method varies from browser to browser. For example, IE is in the format "August 24, 2016 22:26:19"; Sogou is in the format "Wednesday, August 24, 2016 22:39:42"
You can splice the year, month and day of the time separately, so that the compatibility is better:
function getData(n) { let now = new Date(n), y = (), m = () + 1, d = (); return y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d) + " " + ().substr(0, 8); } getData(1642471746435) //'2022-1-18 10:09:06'
3. Popularization of knowledge
1. Current system locale format (toLocaleDateString and toLocaleTimeString)
(new Date()).toLocaleDateString() + " " + (new Date()).toLocaleTimeString() //'2022/1/18 10:30:30 AM'
2. Ordinary strings (toDateString and toTimeString)
(new Date()).toDateString() + " " + (new Date()).toTimeString() //'Tue Jan 18 2022 10:30:50 GMT+0800 (China Standard Time)'
3. Greenwich Standard Time (toGMTString)
(new Date()).toGMTString() //'Tue, 18 Jan 2022 02:31:10 GMT'
4. Global Standard Time (toUTCString)
(new Date()).toUTCString() //'Tue, 18 Jan 2022 02:31:25 GMT'
5. Date object string (toString)
(new Date()).toString() 'Tue Jan 18 2022 10:31:44 GMT+0800 (China Standard Time)'
Date object constructor
Date objects have multiple constructors:
new Date() new Date(milliseconds) new Date(datestring) new Date(year, month) new Date(year, month, day) new Date(year, month, day, hours) new Date(year, month, day, hours, minutes) new Date(year, month, day, hours, minutes, seconds) new Date(year, month, day, hours, minutes, seconds, microseconds)
Description of the parameter of the Date object constructor:
milliseconds - distanceJavaScriptThe internally defined start time1970Year1moon1The number of milliseconds of a day datestring - Date and time represented by strings。This string can be used()Convert year - 四位数的Year份,If the value is0-99,Then add it to it1900 month - 0(代表一moon)-11(代表十二moon)之间的moon份 day - 1-31Date between hours - 0(Represents midnight)-23The number of hours between minutes - 0-59The number of minutes between seconds - 0-59The number of seconds between microseconds - 0-999The number of milliseconds between
Date object returns value
If there are no parameters, the current date will be returned; if the parameter is a number, the number will be treated as a millisecond value, converted to a date. If the parameter is a string, the string will be treated as a string representation of the date, converted to a date. There are also six constructors that can be precisely defined and returned to the time.
var d1 = new Date(); (()); var d2 = new Date("2009-08-08 12:12:12); (()); var d3 = new Date(2009, 8, 8); (());
As a built-in object of JavaScript, Date must be created in new way.
The representation of the Date object in JavaScript is the number of milliseconds (timestamps) from midnight on January 1, 1970 (GMT time). We also call the internal representation of Date here the timestamp.
You can use getTime() to convert the Date object to the timestamp of Date, and the method setTime() can convert the timestamp of Date into the standard form of Date.
Date function usage syntax
date.Method name (parameter 1, parameter 2,...); Date.Method name(); date represents an instance of a date object, Date represents a date object, date.Method name called the member function of the object Date.Static function called the method name called the object
var d=new Date(); var d2=();
Date functions are classified by function
Date get class function
Date() function -- Date对象的构造function getDate() function -- returndateDays in the month in the object(1-31) getDay()function -- returndateThe number of days of the week in the object(0-6) getFullYear() function -- returndateFour-digit years in an object getHours()function -- returndateThe number of hours in the object(0-23) getMilliseconds() function -- returndateNumber of milliseconds in an object(0-999) getMinutes() function -- returndateNumber of minutes in the object(0-59) getMonth() function -- returndateNumber of months in the object(0-11) getSeconds() function -- returndateNumber of seconds in the object(0-59) getTime() function -- returndateTimestamp representation of an object(milliseconds) getTimezoneOffset() function -- return本地时间与用UTCTime difference in the current date,In minutes getUTCDate() function -- returndateWorld Standard Time in Objects(UTC)One day of the month indicated(1-31) getUTCDay() function -- returndateWorld Standard Time in Objects(UTC)A day of the week(0-6) getUTCFullYear() function -- returndateWorld Standard Time in Objects(UTC)Four years indicated getUTCHours() function -- returndateWorld Standard Time in Objects(UTC)The number of hours expressed(0-23) getUTCMilliseconds() function -- returndateWorld Standard Time in Objects(UTC)The number of milliseconds represented(0-999) getUTCMinutes() function -- returndateWorld Standard Time in Objects(UTC)The number of minutes expressed(0-59) getUTCMonth() function -- returndateWorld Standard Time in Objects(UTC)The number of months indicated(0-11) getUTCSeconds() function -- returndateWorld Standard Time in Objects(UTC)The number of seconds expressed(0-59) getYear() function -- returndateYear of the object(Real year minus1900) ()function -- returndateObjects are from World Standard Time(UTC)1970Year1moon1Milliseconds between midnight(Timestamp)
Date setting class function
setDate() function -- set updateDay of the month in the object,And returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setFullYear() function -- set updateObject中的Year份,moon份和sky,And returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setHours() function -- set updateThe subject's hour,minute,Seconds and milliseconds,And returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setMilliseconds() function -- set updateNumber of milliseconds of an object,And returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setMinutes() function -- set updateObject的minute,Second,毫Second,And returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setMonth() function -- set updateObject中moon份,sky,And returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setSeconds() function -- set updateDay of the month in the object,And returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setTime() function -- 使用毫Second数set updateObject,And returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setUTCDate() function -- set updateObject中用世界标准时间(UTC)表示的moon份的一sky,And returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setUTCFullYear() function -- set updateObject中用世界标准时间(UTC)表示的Year份,moon份和sky,And returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setUTCHours() function --- set updateObject中用世界标准时间(UTC)The hours expressed,minute,Seconds and milliseconds,And returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setUTCMilliseconds() function -- set updateObject中用世界标准时间(UTC)表示的毫Second数,And returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setUTCMinutes() function -- set updateObject中用世界标准时间(UTC)表示的minute,Second,And returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setUTCMonth() function -- set updateObject中用世界标准时间(UTC)表示的moon份,sky,And returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setUTCSeconds() function -- set updateObject中用世界标准时间(UTC)表示的Second,毫Second,And returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setYear() function -- set updateObject的Year份(真实Year份减去1900)
Date printing function
toDateString() function -- returndateString representation of the date part of the object toGMTString() function -- returndateGreenwich time of the subject(GMT)string representation toLocaleDateStringfunction -- returndateLocalized string for the date part of the object toLocaleTimeStringfunction -- returndateLocalized string for the time part of the object toTimeString()function -- returndateString of the time part of the object toUTCStringfunction -- returndateWorld Standard Time of Objects(UTC)string representation
Date parsing function
() function -- parses a string of dates,and return to the date distance1970Year1moon1Milliseconds between midnight(Timestamp) JavaScript_Datefunction按照字母分类 Date() function -- DateObject的构造function getDate() function -- returndateObject中的moon份中的sky数(1-31) getDay()function -- returndateThe number of days of the week in the object(0-6) getFullYear() function -- returndateObject中的四位数Year份 getHours()function -- returndateThe number of hours in the object(0-23) getMilliseconds() function -- returndateNumber of milliseconds in an object(0-999) getMinutes() function -- returndateNumber of minutes in the object(0-59) getMonth() function -- returndateObject中的moon份数(0-11) getSeconds() function -- returndateNumber of seconds in the object(0-59) getTime() function -- returndateObject的Timestamp表示法(milliseconds) getTimezoneOffset() function -- return本地时间与用UTCTime difference in the current date,In minutes getUTCDate() function -- returndateWorld Standard Time in Objects(UTC)表示的moon份中的一sky(1-31) getUTCDay() function -- returndateWorld Standard Time in Objects(UTC)A day of the week(0-6) getUTCFullYear() function -- returndateWorld Standard Time in Objects(UTC)表示的四位Year份 getUTCHours() function -- returndateWorld Standard Time in Objects(UTC)The number of hours expressed(0-23) getUTCMilliseconds() function -- returndateWorld Standard Time in Objects(UTC)The number of milliseconds represented(0-999) getUTCMinutes() function -- returndateWorld Standard Time in Objects(UTC)The number of minutes expressed(0-59) getUTCMonth() function -- returndateWorld Standard Time in Objects(UTC)表示的moon份数(0-11) getUTCSeconds() function -- returndateWorld Standard Time in Objects(UTC)The number of seconds expressed(0-59) getYear() function -- returndateObject的Year份(真实Year份减去1900) () function -- parses a string of dates,and return to the date distance1970Year1moon1Milliseconds between midnight(Timestamp) setDate() function -- set updateObject中moon份的一sky,并returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setFullYear() function -- set updateObject中的Year份,moon份和sky,并returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setHours() function -- set updateThe subject's hour,minute,Seconds and milliseconds,并returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setMilliseconds() function -- set updateNumber of milliseconds of an object,并returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setMinutes() function -- set updateObject的minute,Second,毫Second,并returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setMonth() function -- set updateObject中moon份,sky,并returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setSeconds() function -- set updateObject中moon份的一sky,并returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setTime() function -- 使用毫Second数set updateObject,并returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setUTCDate() function -- set updateWorld Standard Time in Objects(UTC)表示的moon份的一sky,并returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setUTCFullYear() function -- set updateWorld Standard Time in Objects(UTC)表示的Year份,moon份和sky,并returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setUTCHours() function --- set updateWorld Standard Time in Objects(UTC)The hours expressed,minute,Seconds and milliseconds,并returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setUTCMilliseconds() function -- set updateWorld Standard Time in Objects(UTC)The number of milliseconds represented,并returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setUTCMinutes() function -- set updateWorld Standard Time in Objects(UTC)表示的minute,Second,并returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setUTCMonth() function -- set updateWorld Standard Time in Objects(UTC)表示的moon份,sky,并returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setUTCSeconds() function -- set updateWorld Standard Time in Objects(UTC)表示的Second,毫Second,并returndateObject distance1970Year1moon1Milliseconds between midnight(Timestamp) setYear() function -- set updateObject的Year份(真实Year份减去1900) toDateString() function -- returndateObject的日期部分string representation toGMTString() function -- returndateObject的格林威治时间(GMT)string representation toLocaleDateStringfunction -- returndateObject的日期部分的本地化字符串 toLocaleTimeStringfunction -- returndateObject的时间部分的本地化字符串 toTimeString()function -- returndateObject的时间部分的字符串 toUTCStringfunction -- returndateObject的世界标准时间(UTC)string representation ()function -- returndateObject distance世界标准时间(UTC)1970Year1moon1Milliseconds between midnight(Timestamp)
4. Javascript timestamp and php timestamp conversion
The timestamp of js is usually 13 bits, and the timestamp of php is 10 bits. The conversion function is as follows:
var nowtime = (new Date).getTime();/*Current timestamp*/ /*Conversion time, calculate the difference*/ function comptime(beginTime,endTime){ var secondNum = parseInt((endTime-beginTime*1000)/1000);// Calculate the difference in timestamp if(secondNum&gt;=0&amp;&amp;secondNum&lt;60){ return secondNum+'Seconds ago'; } else if (secondNum&gt;=60&amp;&amp;secondNum&lt;3600){ var nTime=parseInt(secondNum/60); return nTime+'Minutes ago'; } else if (secondNum&gt;=3600&amp;&amp;secondNum&lt;3600*24){ var nTime=parseInt(secondNum/3600); return nTime+'Hours ago'; } else{ var nTime = parseInt(secondNum/86400); return nTime+'Day Ago'; } } t = comptime("1642471746",nowtime);//1642471746 is the timestamp that PHP is back via ajax, which is 10 bits(t); //27Minutes ago
Replenish:
5 ways to get timestamps in JavaScript
(new Date())
const timestamp = (new Date()); (timestamp); //Output 1591669256000 13Bit
(new Date())
const timestamp = (new Date()); (timestamp); //Output 1591669961203 13Bit
3.(new Date()).valueOf()
const timestamp = (new Date()).valueOf(); (timestamp); //Output 1591670037603 13Bit
Date().getTime()
const timestamp = new Date().getTime(); (timestamp); //Output 1591670068833 13Bit
5.+new Date()
const timestamp = +new Date(); (timestamp); //Output 1591670099066 13Bit
When it is necessary to be accurate to seconds during development, it is recommended to use the first method, which also needs to be divided by 1000. If it is recommended to use the milliseconds of timestamp +new Date() and new Date().getTime();
This is the end of this article about 5 ways to obtain JAVASCRIPT timestamp function. For more related js timestamp function content, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!