[JavaScript] A complete collection of common methods of JavaScript Date objects, such as: getDate, getDay, getFullYear, getHours, etc. (including functions, syntax, parameter analysis, and detailed use cases)

1. getDate() method

Function: It can return a certain day of the month.
Syntax: Date.getDate()
Example:

 let date=new Date()
 let test=date.getDate()
console.log(test);//输出结果为这个月的多少号。比如今天1月15号,输出结果为:15

2. getDay() method

Function: It can return the number of a certain day of the week (0~6). Sunday is 0, Monday is 1, and so on
Syntax: Date.getDay()
Example:

let date=new Date()
let test=date.getDay()
console.log(test);//输出结果根据今天星期几来看,星期天输出0,星期1为1,星期二为2

3. getFullYear() method

Function: It can return a 4-digit number representing the year.
Syntax: Date.getFullYear()
Example:

let date=new Date()
let test=date.getFullYear()
console.log(test);//输出结果为当年年份:比如今年2023年,输出结果为:2023

4. getHours() method

Function: It can return the hour field of the time. The return value is an integer between 0 (midnight) and 23 (11pm).
Syntax: Date.getHours()
Example:

let date=new Date()
let test=date.getHours()
console.log(test);//输出结果为当前时间的24小时制时数。比如先在下午三点钟,返回结果为15

5. getMilliseconds() method

Function: It can return the milliseconds of the time. Returns the milliseconds (0 ~ 999) of the Date object.
Syntax: Date.getMilliseconds()
Example:

let date=new Date()
let test=date.getMilliseconds()
console.log(test);//输出结果为当前时间的毫秒数

6. getMinutes() method

Function: The minute field of the time can be returned. The return value is an integer between 0 and 59.
Syntax: Date.getMinutes()
Example:

let date=new Date()
let test=date.getMinutes()
console.log(test);//输出结果为当前时间的分钟数。比如现在下午4点5分,输出结果为:5

7. getMonth() method

Function: It can return the number representing the month. The return value is an integer between 0 (January) and 11 (December). January is 0, February is 1, and so on.
Syntax: Date.getMonth()
Example:

let date=new Date()
let test=date.getMonth()
console.log(test);//输出结果为当前月份减1。比如先在是12月份,输出结果为11

8. getSeconds() method

Function: The second of the time can be returned. The return value is an integer between 0 and 59.
Syntax: Date.getSeconds()
Example:

let date=new Date()
let test=date.getSeconds()
console.log(test);//输出结果为当前时间的秒数

9. getTime() method

Role: Returns the number of milliseconds from January 1, 1970.
Syntax: Date.getTime()
Example:

let date=new Date()
let test=date.getTime()
console.log(test);//输出结果为距 1970 年 1 月 1 日之间的毫秒数

10. getTimezoneOffset() method

Function: Returns the time difference between Greenwich Mean Time and local time in minutes.
Syntax: Date.getTimezoneOffset()
Example:

let date=new Date()
let test=date.getTimezoneOffset()
console.log(test);//输出结果:-480

11. getUTCDate() method

Function: Returns a day in a month (UTC) according to universal time. Returns the day of the month (a value from 1 to 31).
Syntax: Date.getUTCDate()
Example:

let date=new Date()
let test=date.getUTCDate()
console.log(test);//比如今天2023年3月2号,输出结果:2

12.getUTCDay() method

Function: Returns a number representing the day of the week according to universal time. The value is a value from 0 (Sunday) to 6 (Saturday).
Syntax: Date.getUTCDay()
Example:

et date=new Date()
let test=date.getUTCDay()
console.log(test);//比如今天是星期四,输出结果:4

13. getUTCFullYear() method

Function: Returns the four-digit year expressed according to Universal Time (UTC).
Syntax: Date.getUTCFullYear()
Example:

let date=new Date()
let test=date.getUTCFullYear()
console.log(test);//比如今年是2023年,输出结果:2023

14. getUTCHours() method

Role: Returns the hour of the time according to Universal Time (UTC). The value is an integer between 0 (midnight) and 23 (11pm).
Syntax: Date.getUTCHours()
Example:

let date=new Date()
let test=date.getUTCHours()
console.log(test);//比如现在时间为上午11:30,输出结果:3。如果获取的北京时间,输出结果为11,因为这个方法获取的是世界时,也叫格林尼治时间,需要减去8,得到3。

15. getUTCMilliseconds() method

Function: It can return the milliseconds (0~999) of the time according to the universal time (UTC).
Syntax: Date.getUTCMilliseconds()
Example:

let date=new Date()
let test=date.getUTCMilliseconds()
console.log(test);//输出结果随时都在变,这里就不贴上了

16. getUTCMinutes() method

Function: Return the minute field (0~59) of the time according to Universal Time (UTC).
Syntax: Date.getUTCMinutes()
Example:

let date=new Date()
let test=date.getUTCMinutes()
console.log(test);//比如现在上午11点30分,输出结果:30

17. getUTCMonth() method

Function: Returns a number representing the month. The value is an integer between 0 (January) and 11 (December).
Syntax: Date.getUTCMonth()
Example:

let date=new Date()
let test=date.getUTCMonth()
console.log(test);//比如现在三月份,输出结果为:2

18. getUTCSeconds() method

Function: Return the seconds (0~59) of the time according to the universal time.
Syntax: Date.getUTCSeconds()
Example:

let date=new Date()
let test=date.getUTCSeconds()
console.log(test);//如果当前时间未11点30分20秒,输出结果:20

19. toDateString() method

Function: Convert the date part of the Date object to a string and return the result.
Syntax: Date.toDateString()
Example:

let date=new Date()
let test=date.toDateString()
console.log(test);//输出结果:Thu Mar 02 2023

20.parse()

Function: Parse a date-time string and return the number of milliseconds from midnight on 1970/1/1 to the date-time.
grammar:Date.parse(datestring)

parameter describe
datestring required. A string representing a date and time.

example:

let test=Date.parse("March 21, 2012");
console.log(test);//输出结果:1332259200000

21. setDate() method

Function: used to set a certain day of a month.
grammar:Date.setDate(day)

parameter describe
day required. A value (1 ~ 31) representing the day of the month: 0 is the last day of the previous month, -1 is the day before the last day of the previous month. If the month has 31 days:32 is the first day of the next month. If the current month has 30 days: 32 is the second day of the next month

example:

let date = new Date();
date.setDate(0);
console.log(date);//比如今天是2023年3月4号,输出结果:Tue Feb 28 2023 14:11:20 GMT+0800

22. setFullYear() method

Function: used to set the year. Can be used to set the month and the day of the month.
grammar:Date.setFullYear(year,month,day)

parameter describe
year required. A four-digit integer representing the year. Expressed in local time.
month optional. A numeric value representing the month. Expressed in local time. An integer value between 0 and 11 representing the months January through December. -1 is the last month of the previous year, 12 is the first month of the next year, and 13 is the second month of the next year
day optional. A value (1 ~ 31) representing the day of the month: 0 is the last day of the previous month, -1 is the day before the last day of the previous month. If the month has 31 days:32 is the first day of the next month. If the current month has 30 days: 32 is the second day of the next month

example:

let date = new Date();
date.setFullYear(2022,0,1);//这里代表的是2022年1月1号
console.log(date);//输出结果:Sat Jan 01 2022 14:23:41 GMT+0800

23. setHours() method

Role: used to set the hour field of the specified time. This method can be used to set minutes, seconds and milliseconds.
grammar:Date.setHours(hour,min,sec,millisec)

parameter describe
hour required. Indicates the value of the hour, between 0 (midnight) and 23 (11:00 p.m.), in local time (the same below). -1 is the last hour of yesterday, 24 is the first hour of tomorrow
min optional. Indicates the value of the minute, between 0 and 59. -1 is the last minute of the previous hour, 60 is the first minute of the next hour
sec optional. Indicates the value of seconds, between 0 and 59. -1 is the last second of the previous minute, 60 is the first second of the next minute
millisec optional. A value representing milliseconds, between 0 and 999. -1 is the last millisecond of the previous second, 1000 is the first millisecond of the next second

example:

let date = new Date();
date.setHours(15,35,1,1);//这个代表的意思是下午3点35分1秒1毫秒
console.log(date);//输出结果:Sat Mar 04 2023 15:35:01

24. setMilliseconds() method

Function: used to set the millisecond field of the specified time.
grammar:Date.setMilliseconds(millisec)

parameter describe
millisec required. It is used to set the millisecond field of dateObject, and the parameter is an integer between 0 and 999. -1 is the last millisecond of the previous second, 1000 is the first millisecond of the next second, 1001 is the second millisecond of the next second

example:

let date = new Date();
date.setMilliseconds(555);
let test=date.getMilliseconds()
console.log(test);//输出结果:555

25. setMinutes() method

Function: used to set the minute field of the specified time. This method can also be used to set seconds and milliseconds.
grammar:Date.setMinutes(min,sec,millisec)

parameter describe
min required. A value representing minutes, between 0 and 59, in local time. -1 is the last minute of the previous hour, 60 is the first minute of the next hour
sec optional. Indicates the value of seconds, between 0 and 59. -1 is the last second of the previous minute, 60 is the first second of the next minute
millisec optional. A value representing milliseconds, ranging from 0 to 999. -1 is the last millisecond of the previous second, 1000 is the first millisecond of the next second

example:

let date = new Date();
date.setMinutes(date.getMinutes()-60,30);//时间1小时前,秒数为30
console.log(date);//当前时间为Sat Mar 04 2023 15:31:01,输出结果:Sat Mar 04 2023 14:31:30

26. setMonth() method

Function: used to set the month. Can also be used to set a day of the month.
grammar:Date.setMonth(month,day)

parameter The number of seconds
month required. A numeric value representing the month, which ranges from 0 (January) to 11 (December). -1 is the last month of the previous year, 12 is the first month of the next year, and 13 is the second month of the next year
day optional. A numeric value representing a day of the month, the value is between 1 and 31. 0 is the last day of the previous month, -1 is the day before the last day of the previous month. If the current month has 31 days, 32 is the first day of the next month. If the current month has 30 days, 32 is the second day of the next month

example:

let date = new Date();
date.setMonth(4,20)//表示5月20号
console.log(date);//输出结果

27. setSeconds () method

Function: used to set the second field of the date object. This method can be used to set the number of milliseconds.
grammar:Date.setSeconds(sec,millisec)

parameter describe
sec required. Indicates the value of the second, which is an integer between 0 and 59. -1 is the last second of the previous minute, 60 is the first second of the next minute
millisec optional. A value representing milliseconds, between 0 and 999

example

let date = new Date();
date.setSeconds(35,825)
console.log(date+":"+date.getMilliseconds());
//输出结果:Sat Mar 04 2023 16:26:35 GMT+0800 (台北标准时间):825

28. setTime() method

Role: Set the Date object in milliseconds.
grammar:Date.setTime(millisec)

parameter describe
millisec required. The date and time to set in milliseconds between midnight January 1, 1970 GMT. This type of millisecond value can be passed to the Date() constructor, which can be obtained by calling the Date.UTC() and Date.parse() methods. Representing a date in milliseconds makes it time zone independent.

example:

let date = new Date();
date.setTime(-1332403882588);
console.log(date);//输出结果:Wed Oct 12 1927 23:48:37 GMT+0800

29. setUTCDate() method

Function: Returns a day in a month (UTC) according to universal time.
Syntax and examples refer to the setDate() method

30.setUTCFullYear() method

Function: Set the year according to Universal Time (UTC).
Syntax and examples refer to the setFullYear() method

31. setUTCHours() method

Function: Set the hour (0 - 23) according to Universal Time (UTC). This method can be used to set minutes, seconds and millimeters.
Syntax and examples refer to setHours() method

32. setUTCMilliseconds() method

Function: It is used to set the milliseconds of the specified time according to Universal Time (UTC).
Syntax and examples refer to setMilliseconds() method

33. setUTCMinutes() method

Function: used to set the minutes of the specified time according to Universal Time (UTC).
Refer to the setMinutes() method for syntax and examples

34. setUTCMonth() method

Function: used to set the month according to Universal Time (UTC). This method can be applied to set a certain day of the month.
Syntax and examples refer to the setMonth() method

35. setUTCSeconds() method

Role: Used to set the seconds field of the specified time according to Universal Time (UTC).
Syntax and examples refer to the setSeconds() method

36. toISOString() method

Role: Date objects can be converted to strings using the ISO standard.
Syntax: Date.toISOString()
Example:

let date = new Date();
let test=date.toISOString()
console.log(test);输出结果类似:2023-03-04T09:23:21.817Z

37. toJSON() method

Function: Convert the Date object to a string and format it into JSON data format.
Syntax: Date.toJSON()
Example:

let date = new Date();      
let test=date.toJSON()
console.log(test);//输出结果类似:2023-03-04T09:27:25.284Z

38. toLocaleDateString() method

Function: Convert the date part of the Date object to a string according to the local time, and return the result
Syntax: Date.toLocaleDateString()
Example:

let date=new Date();
let test=date.toLocaleDateString();
console.log(test)//比如今天时间是2023年3月5号,输出结果:2023/02/05

39. toLocaleTimeString() method

Function: Convert the time part of the Date object to a string according to the local time, and return the result.
Syntax: Date.toLocaleTimeString()
Example:

let date=new Date();
let test=date.toLocaleTimeString();
console.log(test)//比如现在时间为是2023年3月5号下午8点48分10秒,输出结果:下午8:48:10

40. toLocaleString () method

Function: Convert the Date object to a string according to the local time, and return the result.
Syntax: Date.toLocaleString()
Example:

let date=new Date();
let test=date.toLocaleString();
console.log(test)//比如现在结果时间为2023年3月5号下午8点48分10秒,输出结果:2023/3/5 下午8:48:10

41. toString() method

Function: Convert the Date object to a string and return the result.
Syntax: Date.toString()
Example:

let date=new Date();
let test=date.toString();
console.log(test)//输出结果:Sun Mar 05 2023 21:00:04 GMT+0800 (中国标准时间)

42. toTimeString() method

Function: Convert the time part of the Date object to a string and return the result.
Syntax: Date.toTimeString()
Example:

let date=new Date();
let test=date.toTimeString();
console.log(test);//输出结果类似于:21:14:35 GMT+0800 (中国标准时间)

43. toUTCString() method

Function: Convert the Date object to a string according to Universal Time (UTC), and return the result.
Syntax: Date.toUTCString()
Example:

let date=new Date();
let test=date.toUTCString();
console.log(test);//输出结果类似于:Sun, 05 Mar 2023 13:22:26 GMT

44.UTC() Method

Function: Return the number of milliseconds from January 1, 1970 to the specified date according to universal time
Syntax: Date.UTC(year,month,day,hours,minutes,seconds,millisec)

parameter describe
year required. Four digits representing the year.
month required. An integer representing the month, ranging from 0 to 11.
day required. An integer representing the date, ranging from 1 to 31.
hours optional. An integer representing the hour, ranging from 0 to 23.
minutes optional. An integer representing minutes, ranging from 0 to 59.
seconds optional. An integer representing seconds, ranging from 0 to 59.
ms optional. An integer representing milliseconds, ranging from 0 to 999.

example:

let test=Date.UTC(2023,1,1)
console.log(test);输出结果:1675209600000

45. valueOf() method

Function: Return the original value of the Date object. The original value returns the number of milliseconds since midnight, January 1, 1970!
Syntax: Date.valueOf()
Example:

let date=new Date();
let test=date.valueOf();
console.log(test);//输出值类似:1678028548436

46. ​​getYear () method ----- has been disabled

Obsolete. Please use the getFullYear() method instead.

47.setYear() method ----- has been disabled

已废弃。请使用 setFullYear() 方法代替。

48.toGMTString()

已废弃。请使用 toUTCString() 方法代替。

结论

  这篇文章主要总结了JavaScript String对象常用的一些方法,如果哪里有错误或者不完善的地方,欢迎各位指教,谢谢!

Guess you like

Origin blog.csdn.net/m0_46533551/article/details/129257474
Recommended