Date common methods:
Returns the current date and time: Date()
console.log(Date());//The result is Sat Jul 01 2017 10:14:11 GMT+0800 (China Standard Time)
This is the only way to use direct output without having to mount the object, but sometimes this format is not what we want, so it is not used directly.
How to get the year, month, day, week, hour, minute, second:
getFullYear(),getMonth(),getDate(),getDay(),getHours(),getMinutes(),getSeconds();
//These methods need to be mounted on an object to be able to use
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth()+1;
var day = date.getDate();
var week = date.getDay();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
console.log(year);//The result is 2017
console.log(month);//The result is 7
console.log(day);//The result is 1
console.log(week);//The result is 6
console.log(hour);//The result is 10
console.log(minute);//The result is 24
console.log(second);//The result is 24
It is important to note that getMonth() returns 0-11, so you need to add 1 to correct the display of the month. Both getMonth() and getDate() return a single digit before they are less than 10. If you want to achieve two digits, you need to make your own judgment to add the previous 0; getDay() also returns a display from 0-6 Sundays to Saturday, and if you want to convert to another form, you need to do it yourselfArray to call display.
Method for setting year, month, day, hour, minute, second:
setFullYear(),setMonth(),setDate(),setHours(),setMinutes(),setSeconds();
var date = new Date();
var year = date.setFullYear(2016);
var month = date.setMonth(10);
var day = date.setDate(23);
var hour = date.setHours(18);
var minute = date.setMinutes(50);
var second = date.setSeconds(40);
console.log(date.getFullYear());//The result is 2016
console.log(date.getMonth());//The result is 10
console.log(date.getDate());//The result is 23
console.log(date.getHours());//The result is 18
console.log(date.getMinutes());//The result is 50
console.log(date.getSeconds());//The result is 40
Since the values returned by all setup methods are milliseconds after setup and cannot be read effectively, you need to call the get method again to display the modified time values.There is no way to set the day of the week.
Get the number of milliseconds from midnight 1970/1/1 to that date time: parse()
var minutes = 1000 * 60;
var hours = minutes * 60;
var days = hours * 24;
var years = days * 365;
var t = Date.parse("2017/7/1");
var y = t/years;
console.log(y);//The result was 47.52785388127854
Date.parse() is the static method of the Date object.This method is typically called as Date.parse(), not as dateobject.parse().
Gets the number of milliseconds between the current date and time interval at midnight on January 1, 1970 (GMT time).: getTime()
var minutes = 1000 * 60
var hours = minutes * 60
var days = hours * 24
var years = days * 365
var date = new Date();
var t = date.getTime();
var y = t/years;
console.log(y);//The result is 47.52909686998985
This method has no parameters passed in and returns milliseconds.setTime(millisec) sets the value of Date in milliseconds, the parameter is required, and the return value is the set millisec.