js Date对象 (个人学习笔记)

Date对象用于处理日期和 时间

1.直接获取当前的时间为标准时间

var date = new Date();//Wed May 30 2018 14:25:30 GMT+0800 (中国标准时间)
var date1 = new Date("January 12,2006 22:19:35")  // 可以传入一个时间  进行转化
console.log(date1)  //Thu Jan 12 2006 22:19:35 GMT+0800 (中国标准时间)

2.常用的对象方法

  get方法

     date.getFullYear()  //2017  从 Date 对象以四位数字返回年份
     date.getMonth()  //11  从 Date 对象返回月份 (0 ~ 11) 需要加1才是我们正常的月份
     date.getDay()  //2  从 Date 对象返回一周中的某一天 (0 ~ 6) 是星期几
     date.getDate()  //12  从 Date 对象返回一个月中的某一天 (1 ~ 31
     date.getHours()  //23  返回 Date 对象的小时 (0 ~ 23)
     date.getMinutes()  //30  返回 Date 对象的分钟 (0 ~ 59)
     date.getSeconds()  //54  返回 Date 对象的秒数 (0 ~ 59)
     date.getMilliseconds()  返回 Date 对象的毫秒(0 ~ 999)
     date.getTime()  返回 1970 年 1 月 1 日至今的毫秒数

常用场景:时间戳

  set方法   

    setFullYear(year, opt_month, opt_date) :设置Date对象的年份值;4位年份。
     setMonth(month, opt_date) :设置Date对象的月份值。0表示1月,11表示12月。
     setDate(date) :设置Date对象的月份中的日期值;值的范围1~31 。
     setHours(hour, opt_min, opt_sec, opt_msec) :设置Date对象的小时值。
     setMinutes(min, opt_sec, opt_msec) :设置Date对象的分钟值。
     setSeconds(sec, opt_msec) :设置Date对象的秒数值。
     setMilliseconds(msec) :设置Date对象的毫秒值。

  其他方法   

     toString() :将Date转换为一个'年月日 时分秒'字符串
     toLocaleString() :将Date转换为一个'年月日 时分秒'的本地格式字符串
     toDateString() :将Date转换为一个'年月日'字符串
     toLocaleDateString() :将Date转换为一个'年月日'的本地格式字符串
     toTimeString() :将Date转换为一个'时分秒'字符串
     toLocaleTimeString() :将Date转换为一个'时分秒'的本地格式字符串
     valueOf() :与getTime()一样, 返回Date对象与'1970/01/01 00:00:00'之间的毫秒值(北京时间的时区为东8区,起点时间实际为:'1970/01/01 08:00:00')

原文地址:https://www.cnblogs.com/web-chuan/p/9110871.html