The meaning of a time stamp
Time stamp refers to the total number of seconds GMT January 01, 1970 00:00 00 seconds (Beijing time January 01, 1970 08:00 00 seconds) up to now.
Two, 1. JS Gets the current timestamp method:
var timestamp = date.parse (new Date ()),//deprecated, because the millisecond level value is converted to 000, inaccurate!
var timestamp = (new Date ()). ValueOf ();//Gets the timestamp of the current millisecond, exactly!
var timestamp = new Date (). Get time ();//return value unit is milliseconds;
How to get the specified timestamp in 2.js
var timestamp = (New Date ("2018/06/22 08:00:20")). GetTime ()/1000;
Iii. time conversion into timestamps
var timestamp= new Date (1472025255555)//directly with the new date (timestamp to convert);
Note that the timestamp must be int type, otherwise the conversion fails. parseint () a bit better.
Iv. Date of format
The development of the long use to date, but you new date () or obtained can not be directly used to display to the user, so generally we will write a method of formatting the date
As follows:
Formatted date
function DateFormat (thisdate, fmt) {var o = {"m+": Thisdate.getmonth () + 1, "d+": thisdate.getdate (), "H +": thisdate.gethours (), "m+": Thisdate.getminutes (), "s+": Thisdate.getseconds (), "q+": Math . Floor ((Thisdate.getmonth () + 3)/3), "S": Thisdate.getmilliseconds ()}; if (/(y+)/.test (FMT)) FMT = Fmt.replace (regexp.$1, (thisdate.getfullyear () + ""). substr (4-regexp.$1.length)); For (var k in O) if (New RegExp ("(" + K + ")"). Test (FMT)) FMT = Fmt.replace (regexp.$1, (regexp.$1.lengt h = = 1)? (O[k]): (("XX" + o[k]). substr (("" + O[k]).)); return FMT;}
var date=new Date ();
Console.log (DateFormat (Date, "Yyyy-mm-dd hh:mm:ss")//Call
Five, according to the date to calculate the week
1. Calculate the current date for Monday and Sunday
var date=new Date ();
var selecttime = Date.gettime ();//Get timestamp
var selectday = Date.getday ();//Get week var onedaylong = 24 * 3600 * 1000;//defines how many milliseconds a day is var mondaytime = selecttime-(SE LECTDAY-1) * Onedaylong;
var Monday = new Date (mondaytime)//timestamp back to TIME
var sundaytime = selecttime-(selectDay-7) * Onedaylong;
var sundaytime = new Date (sundaytime)
Console.log (Monday)
Console.log (Sundaytime)
2. Calculate the day of the week
When we know a certain ' yyyy-mm-dd ' date we can calculate the current day of the week, the formula is as follows:
Kimlarsson Calculation formula
W= (d+2*m+3* (m+1)/5+y+y/4-y/100+y/400) MoD 7
In the formula, D represents the number of days in a date, m represents the number of months, and Y represents the number of years. (which mod means to take the remainder calculation, JS with%)
JS Timestamp in Get mode