创建 Date 对象的方法
(1)创建当前(现在)日期对象的实例,不带任何参数
var today = new Date();
(2)创建指定时间戳的日期对象实例,参数是时间戳。
时间戳:是指某一个时间距离 1970 年 1 月 1 日 0 时 0 分 0 秒,过去了多少毫秒值(1 秒
=1000 毫秒)
var timer = new Date(10000); //时间是 1970 年 1 月 1 日 0 时 0 分 10 秒
(3)指定一个字符串的日期时间信息,参数是一个日期时间字符串
var timer = new Date(“2015/5/25 10:00:00”);
(4)指定多个数值参数
var timer = new Date(2015+100,4,25,10,20,0); //顺序为:年、月、日、 时、分、秒,年、月、日是必须的
方法:
Date.getDate( ) 返回一个月中的某一天
Date.getDay( ) 返回一周中的某一天
Date.getFullYear( ) 返回 Date 对象的年份字段
Date.getHours( ) 返回 Date 对象的小时字段
Date.getMilliseconds( ) 返回 Date 对象的毫秒字段
Date.getMinutes( ) 返回 Date 对象的分钟字段
Date.getMonth( ) 返回 Date 对象的月份字段
Date.getSeconds( ) 返回 Date 对象的秒字段
Date.getTime( ) 返回 Date 对象的毫秒表示
Date.getTimezoneOffset( ) 判断与 GMT 的时间差
Date.getUTCDate( ) 返回该天是一个月的哪一天(世界时)
Date.getUTCDay( ) 返回该天是星期几(世界时)
Date.getUTCFullYear( ) 返回年份(世界时)
Date.getUTCHours( ) 返回 Date 对象的小时字段(世界时)
Date.getUTCMilliseconds( ) 返回 Date 对象的毫秒字段(世界时)
Date.getUTCMinutes( ) 返回 Date 对象的分钟字段(世界时)
Date.getUTCMonth( ) 返回 Date 对象的月份(世界时)
Date.getUTCSeconds( ) 返回 Date 对象的秒字段(世界时)
Date.getYear( ) 返回 Date 对象的年份字段(世界时)
Date.parse( ) 解析日期/时间字符串
Date.setDate( ) 设置一个月的某一天
Date.setFullYear( ) 设置年份,也可以设置月份和天
Date.setHours( ) 设置 Date 对象的小时字段、分钟字段、秒字段和毫秒字段 Date.setMilliseconds( ) 设置 Date 对象的毫秒字段
Date.setMinutes( ) 设置 Date 对象的分钟字段和秒字段
Date.setMonth( ) 设置 Date 对象的月份字段和天字段
Date.setSeconds( ) 设置 Date 对象的秒字段和毫秒字段
Date.setTime( ) 以毫秒设置 Date 对象
Date.setUTCDate( ) 设置一个月中的某一天(世界时)
Date.setUTCFullYear( ) 设置年份、月份和天(世界时)
Date.setUTCHours( ) 设置 Date 对象的小时字段、分钟字段、秒字段和毫秒字段(世界 时)
Date.setUTCMilliseconds( ) 设置 Date 对象的毫秒字段(世界时)
Date.setUTCMinutes( ) 设置 Date 对象的分钟字段和秒字段(世界时) Date.setUTCMonth( ) 设置 Date 对象的月份字段和天数字段(世界时) Date.setUTCSeconds( ) 设置 Date 对象的秒字段和毫秒字段(世界时)
Date.setYear( ) 设置 Date 对象的年份字段
Date.toDateString( ) 返回 Date 对象日期部分作为字符串
Date.toGMTString( ) 将 Date 转换为世界时字符串
Date.toLocaleDateString( ) 回 Date 对象的日期部分作为本地已格式化的字符
串
Date.toLocaleString( ) 将 Date 转换为本地已格式化的字符串 Date.toLocaleTimeString( ) 返回 Date 对象的时间部分作为本地已格式化的字符串 Date.toString( ) 将 Date 转换为字符串
Date.toTimeString( ) 返回 Date 对象日期部分作为字符串
Date.toUTCString( ) 将 Date 转换为字符串(世界时)
Date.UTC( ) 将 Date 规范转换成毫秒数
Date.valueOf( ) 将 Date 转换成毫秒表示
获取完整的日期(默认格式):
var date = new Date; // Sat Jul 06 2019 19:59:27 GMT+0800 (中国标准时间)
获取当前年份:
var year = date.getFullYear(); // 2019 获取当前月份: var month = date.getMonth() + 1; // 7
获取当前日:
var day = date.getDay(); // 6
获取当前日期(年-月-日): month = (month > 9) ? month : ("0" + month); day = (day < 10) ? ("0" + day) : day;var today = year + "-" + month + "-" + day; // 2019-07-06
另外的一些操作:
date.getYear(); // 获取当前年份(2 位)
date.getFullYear(); // 获取完整的年份(4 位, 1970-????)
date.getMonth(); // 获取当前月份(0-11,0 代表 1 月)
date.getDate(); // 获取当前日(1-31)
date.getDay(); // 获取当前星期 X(0-6,0 代表星期天)
date.getTime(); // 获取当前时间(从 1970.1.1 开始的毫秒数)
date.getHours(); // 获取当前小时数(0-23)
date.getMinutes(); // 获取当前分钟数(0-59)
date.getSeconds(); // 获取当前秒数(0-59)
date.getMilliseconds(); // 获取当前毫秒数(0-999)
date.toLocaleDateString(); // 获取当前日期
date.toLocaleTimeString(); // 获取当前时间
date.toLocaleString( ); // 获取日期与时间
网友评论