JavaScript - Date Object

First, what is the Date object

Js used in the Date object to represent a time, Date is a function object

Second, create a Date object

Two ways to create a Date object

  • Constructor creates the current time
  • Create a string parameter specified time

1. The constructor creates the current object

var d1 = new Date();

At this time of the current package d1 code execution

2. Create a string parameter specified time

var d= new Date("01/02/2015 11:10:10");

Incoming string format of month / day / year hours: minutes: seconds

Third, the common method of Date object

getDate()

  • Get the current date is a few numbers
<script type="text/javascript">
	var d= new Date("01/02/2015 11:10:10");
	var date= d.getDate();
	console.log("date="+date);//输出date=2
</script>

There is also a similar method, the usage is similar

  • getDay (); Gets the current date for the week, 0 represents Sunday
  • getMonth (); target acquisition date a few months, 0 for January
  • getFullYear (); Gets Year

Fourth, the next time stamp highlights

4.1 What is the timestamp
It refers to the number of milliseconds from January 1, 1970 00:00:00 Greenwich Mean Time to the current date of the

4.2 Why have a time stamp
time of binary complex (eg, 1min = 60sec, 60 hex day = 24h, 24 hex, January = 28,29,30,31 days, more complicated), so in the computer would be more trouble when the storage time, so,In the bottom of the computer all the time to milliseconds

4.3 How to get the timestamp
getTime by Date object () method

<script type="text/javascript">
	var d= new Date("01/02/2015 11:10:10");
	var time= d.getTime();
	console.log("time="+time);//输出time=1420168210000
</script>

4.4 timestamp role - Performance test code

<script type="text/javascript">
	var start =Date.now();//now方法得到现在的时间戳
	for(var i=0;i<100;i++){
	   console.log(i);
	}
	var end = Date.now();
	var result = end-start;
	console.log("执行了,"+result+"毫秒");//执行了,7毫秒
</script>
He published 198 original articles · won praise 94 · views 90000 +

Guess you like

Origin blog.csdn.net/shang_0122/article/details/104661504