JavaScript date object (Date)

1. Description

  • Creates a JavaScript Date instance that represents a moment in time. Date objects are based on the Unix Time Stamp, which is the number of milliseconds since January 1, 1970 (UTC).
    ——————— Quoted from the official website

2. Grammar

(1) Get the current time

① Syntax format:

	let 变量名 = new Date;
	let 变量名 = Date.now();
	//输出变量内容
	//输出变量类型

② Example:

	<script>
		//获取当前日期
		let date = new Date;
		console.log(date);
		console.log(typeof date);
		
		//获取当前日期毫秒数
		let dates = Date.now();
		console.log(dates);
		console.log(typeof dates);
	</script>

③ Operation effect

Picture

(2) Specified time

② Syntax format:

	let 变量名 = new Date(指定的时间);
	//输出变量内容
	//输出变量类型

② Example:

	<script>
		//方法1
		let dateOne = new Date('2023-01-28');
		console.log(dateOne);
		console.log(typeof dateOne);
	
		//方法2
		let dateTwo = new Date('January 28,2023 15:00:00');
		console.log(dateTwo);
		console.log(typeof dateTwo);

		//方法3
		let dateThr = new Date('2023-01-28T15:00:00');
		console.log(dateThr);
		console.log(typeof dateThr);

		//方法4
		let dateFou = new Date(2023,01,28);
		console.log(dateFou);
		console.log(typeof dateFou);
	
		//方法5
		let dateFiv = new Date(2023,01,28,15,0,0);
		console.log(dateFiv);
		console.log(typeof dateFiv);
	</script>

③ Operation effect

Picture

3. Method

(1) parse(): returns the number of milliseconds of a certain date

① Syntax format:

	let 变量名 = Date.parse('要转换的时间');
	//输出变量结果

② Example:

	<script>
		let date = Date.parse('2023-01-28T15:00:00');
		console.log(date);
	</script>

③ Operation effect

Picture

(2) getFullYear(): get the year

① Syntax format:

	let 变量名 = new Date('日期');
	//输出变量结果并获取年

② Example:

	<script>
		let date = new Date('2023-01-28');
		console.log(date.getFullYear());
	</script>

③ Operation effect

Picture

(3) getMonth(): Get the month

  • The return value is 0 ~ 11, which needs to be +1 on the basis of the return value;

① Syntax format:

	let 变量1 = new Date('日期');
	let 变量2 = 变量1.getMonth() + 1;
	//输出变量2结果并获取月

② Example:

	<script>
		let date = new Date('2023-01-28');
		let month = date.getMonth() + 1;
		console.log(month);
	</script>

③ Operation effect

Picture

(4) getDate(): Get the day

  • Return value: 1 ~ 31

① Syntax format:

	let 变量名 = new Date('日期');
	//输出变量结果并获取天

② Example:

	<script>
		let date = new Date('2023-01-28');
		console.log(date.getDate());
	</script>

③ Operation effect

Picture

(5) getDay(): get the day of the week

  • The return value is 0 ~ 6, 0 means Sunday

① Syntax format:

	let 变量名 = new Date('日期');
	//输出变量结果并获取星期

② Example:

	<script>
		let date = new Date('2023-01-28');
		console.log(date.getDay());
	</script>

③ Operation effect

Picture

(6) getHours(): get hours

① Syntax format:

	let 变量名 = new Date('日期');
	//输出变量结果并获取小时

② Example:

	<script>
		let date = new Date('2023-01-28T15:05:03');
		console.log(date.getHours());
	</script>

③ Operation effect

Picture

(7) getMinutes(): Get minutes

① Syntax format:

	let 变量名 = new Date('日期');
	//输出变量结果并获取分钟

② Example:

	<script>
		let date = new Date('2023-01-28T15:05:03');
		console.log(date.getMinutes());
	</script>

③ Operation effect

Picture

(8) getSeconds(): get seconds

① Syntax format:

	let 变量名 = new Date('日期');
	//输出变量结果并获取秒

② Example:

	<script>
		let date = new Date('2023-01-28T15:05:03');
		console.log(date.getSeconds());
	</script>

③ Operation effect

Picture

(9) getMilliseconds(): Get milliseconds

① Syntax format:

	let 变量名 = new Date('日期');
	//输出变量结果并获取毫秒

② Example:

	<script>
		let date = new Date();
		console.log(date.getMilliseconds());
	</script>

③ Operation effect

Picture

(10) Get local time

① Syntax format:

	let 变量1 = toLocaleDateString(); //获取日期
	let 变量2 = toLocaleTimeString(); //获取时间
	let 变量3 = toLocaleString(); //获取日期 时间
	console.log(变量1);
	console.log(变量2);
	console.log(变量3);

② Example:

	<script>
		let dates = new Date();
		let date = dates.toLocaleDateString(); //获取日期
		let time = dates.toLocaleTimeString(); //获取时间
		let dateTime = dates.toLocaleString(); //获取日期/时间
		console.log(date);
		console.log(time);
		console.log(dateTime);
	</script>

③ Operation effect

Picture

(11) Modify the date display format

① Syntax format:

	let 变量名 = new Date('日期');
	函数名(变量名); //调用函数
	function 函数名(形参){
    
    
		//获取年;
		//获取月;
		//获取日;
		return+ '-' ++ '-' +;
	}

② Example:

	<script>
		//获取时间
		let date = new Date();
		//输出获取日期的原始值
		console.log(date);
		//调用函数并赋值给result
		let result = DateFormatString(date);
		//输出函数执行结果
		console.log(result);
		//定义函数
		function DateFormatString(date){
    
    
			//获取年
			let year = date.getFullYear();
			
			//获取月
			let month = date.getMonth() + 1;
			month = (month > 10)?month:'0' + month;	//月小于10,在数字前加0
			
			//获取天
			let day = date.getDate();
			day = (day > 10)?day:'0' + day	//天小于10,在数字前加0
			
			//获取时间
			let time = date.toLocaleTimeString();
			
			//返回值
			return year + '-' + month + '-' + day + ' ' + time
		}
	</script>

③ Running results

Picture

For more date object learning, please refer to the official website documentation

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date

Guess you like

Origin blog.csdn.net/StupidlyGrass/article/details/128778071