JS获取当前日期
- var myDate = new Date();
- myDate.getYear(); //获取当前年份(2位)
- myDate.getFullYear(); //获取完整的年份(4位,1970-???)
- myDate.getMonth(); //获取当前月份(0-11,0代表1月)
- myDate.getDate(); //获取当前日(1-31)
- myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
- myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
- myDate.getHours(); //获取当前小时数(0-23)
- myDate.getMinutes(); //获取当前分钟数(0-59)
- myDate.getSeconds(); //获取当前秒数(0-59)
- myDate.getMilliseconds(); //获取当前毫秒数(0-999)
- myDate.toLocaleDateString(); //获取当前日期
- var mytime=myDate.toLocaleTimeString(); //获取当前时间
- myDate.toLocaleString( ); //获取日期与时间
时间函数 - setDate() 设置 Date 对象中月的某一天 (1 ~ 31)
- setMonth() 设置 Date 对象中月份 (0 ~ 11)
- setFullYear() 设置 Date 对象中的年份(四位数字)
- setYear() 请使用 setFullYear() 方法代替
- setHours() 设置 Date 对象中的小时 (0 ~ 23)
- setMinutes() 设置 Date 对象中的分钟 (0 ~ 59)
- setSeconds() 设置 Date 对象中的秒钟 (0 ~ 59)
- setMilliseconds() 设置 Date 对象中的毫秒 (0 ~ 999)
- setTime() 以毫秒设置 Date 对象
格式转化 - toSource() 返回该对象的源代码
- toString() 把 Date 对象转换为字符串
- toTimeString() 把 Date 对象的时间部分转换为字符串
- toDateString() 把 Date 对象的日期部分转换为字符串
- toGMTString() 请使用 toUTCString() 方法代替
- toUTCString() 根据世界时,把 Date 对象转换为字符串
- toLocaleString() 根据本地时间格式,把 Date 对象转换为字符串
- toLocaleTimeString() 根据本地时间格式,把 Date 对象的时间部分转换为字符串
- toLocaleDateString() 根据本地时间格式,把 Date 对象的日期部分转换为字符串
案例
将指定的日期转换为 年月日 的格式
var oldTime = (new Date(