Date let now = new Date()
获取日期组件
now.getFullYear() //获取年份2008
now.getMonth() //获取月份,0~11
now.getDate() //获取天,1~31
now.getHours()
now.getMinutes()
now.getSeconds()
now.getMilliseconds()
now.getDay() //0~6,周日到周六
now.getTime() //获取从1970至今的毫秒数(优先)
alert( +now ) //获取从1970至今的毫秒数
Date.now() //获取从1970至今的毫秒数
设置日期组件
Date : var now = new Date ();var sometime = new Date (Date .parse ("7/10/2019" ));var sometime = new Date (Date .UTC (2018 ,6 ,10 )); getTime (); getFullYear (); getMonth (); getDate (); getDay (); getHours (); getMinutes (); getSeconds (); getMilliSeconds ();
new Date (0 ) new Date (24 * 60 * 60 ) new Date (Time : number )new Date (date : string )new Date (year, month, date, hours, minutes, seconds, ms)
方法 获取时间 let date = new Date (); date.getFullYear () data.getMonth () data.getDate () getHours (),getMinutes (),getSeconds (),getMilliseconds () date.getDay () date.getTime ()
设置时间 new Date ().setFullYear (year, [month], [date])new Date ().setMonth (month, [date])new Date ().setDate (date)new Date ().setHours (hour, [min], [sec], [ms])new Date ().setMinutes (min, [sec], [ms])new Date ().setSeconds (sec, [ms])new Date ().setMilliseconds (ms)new Date ().setTime (milliseconds)(使用自 1970 -01 -01 00 :00 :00 UTC +0 以来的毫秒数来设置整个日期)
具体应用 本月第一天到 最后一天 const nextMonth = new Date ().getMonth () + 1 ; const nextMonthFirstDay = new Date (new Date ().getFullYear (), nextMonth, 1 ); const oneDay = 24 * 60 * 60 * 1000 ;let startDate = new Date (new Date (new Date ().setDate (1 )).setHours (0 , 0 ,0 , 0 ));let endDate = new Date (new Date (new Date (nextMonthFirstDay - oneDay))setHours (23 , 59 , 59 , 999 ));
本周第一天到 最后一天 const startTime = new Date ().setHours (0 , 0 , 0 , 0 );const endTime = new Date ().setHours (23 , 59 , 59 , 999 );let nowDayOfWeek = new Date ().getDay ();if (nowDayOfWeek === 0 ) nowDayOfWeek = 7 ;const dayNum = 1 * 24 * 60 * 60 * 1000 ;const firstDate = new Date (new Date ().valueOf () - (nowDayOfWeek - 1 ) *dayNum);const lastDate = new Date (new Date (firstDate).valueOf () + 6 * dayNum);const startDate = new Date (startTime (firstDate));const endDate = new Date (endTime (lastDate));
今日零点到24点 const startDate = new Date (new Date ().setHours (0 , 0 , 0 , 0 ));const endDate = new Date (new Date ().setHours (23 , 59 , 59 , 999 ));
日期转数字 let date : Date = new Date (); date.valueOf (); date.getTime (); Date .now (); +date; new Date (2022 ,6 ,25 )-new Date (2022 ,6 ,24 ) new Date (2022 ,6 ,25 ).getTime ()-new Date (2022 ,6 ,24 ).getTime ()
创建日期 创建一个 Date 对象,日期是:Feb 20, 2012, 3:12am。时区是当地时区。
1. new Date (2012 ,1 ,20 ,3 ,12 );2. new Date ("2012-02-20T03:12" );