js: Date对象的使用方法:
function Date() // 获取当前时间戳 now: function now() // 解析时间格式的字符串为时间戳 parse: function parse() prototype: constructor: function Date() // 获取完整的年份(4位) getFullYear: function getFullYear() // 获取年份(2位) getYear: function getYear() // 获取月份(0-11, 0代表1月) getMonth: function getMonth() // 获取日(1-31) getDate: function getDate() // 获取星期(0-6,0代表星期天) getDay: function getDay() // 获取小时数(0-23) getHours: function getHours() //获取分钟数(0-59) getMinutes: function getMinutes() // 获取秒数(0-59) getSeconds: function getSeconds() // 获取毫秒数(0-999) getMilliseconds: function getMilliseconds() // 获取时间戳 (从1970.1.1开始的毫秒数) getTime: function getTime() // 获取时间戳 valueOf: function valueOf() getTimezoneOffset: function getTimezoneOffset() getUTCDate: function getUTCDate() getUTCDay: function getUTCDay() getUTCFullYear: function getUTCFullYear() getUTCHours: function getUTCHours() getUTCMilliseconds: function getUTCMilliseconds() getUTCMinutes: function getUTCMinutes() getUTCMonth: function getUTCMonth() getUTCSeconds: function getUTCSeconds() setDate: function setDate() setFullYear: function setFullYear() setHours: function setHours() setMilliseconds: function setMilliseconds() setMinutes: function setMinutes() setMonth: function setMonth() setSeconds: function setSeconds() setTime: function setTime() setUTCDate: function setUTCDate() setUTCFullYear: function setUTCFullYear() setUTCHours: function setUTCHours() setUTCMilliseconds: function setUTCMilliseconds() setUTCMinutes: function setUTCMinutes() setUTCMonth: function setUTCMonth() setUTCSeconds: function setUTCSeconds() setYear: function setYear() toDateString: function toDateString() toGMTString: function toUTCString() toISOString: function toISOString() toJSON: function toJSON() toLocaleDateString: function toLocaleDateString() toLocaleString: function toLocaleString() toLocaleTimeString: function toLocaleTimeString() toString: function toString() toTimeString: function toTimeString() toUTCString: function toUTCString()
示例:
// 获取时间对象 new Date() // 2021-10-29T03:04:45.640Z // 通过时间格式字符串获取时间对象 new Date('2021-10-29 03:00:00') // 2021-10-29T03:00:000Z // 获取时间戳 Date.now() // 1635476685643 // 解析时间格式字符串获取时间戳 Date.parse('2021-10-29 03:00:00') // 1635447600000
更多用法参考