js獲取日期
常用如下:
var date = new Date();//中國(guó)標(biāo)準(zhǔn)時(shí)間var year = date.getFullYear();//獲取完整的年份(4位)var month = date.getMonth();//獲取當(dāng)前月份(0-11,0代表1月)var nowDate = date.getDate();//獲取當(dāng)前日(1-31)var day = date.getDay();//獲取當(dāng)前星期X(0-6,0代表星期天)
更多請(qǐng)點(diǎn)擊JavaScript 標(biāo)準(zhǔn)庫(kù) Date或參考中第一篇。
日期的相關(guān)方法
格式化日期
1、yyyy-MM-dd
//格式化日期:yyyy-MM-ddfunction formatDate(date) {var myyear = date.getFullYear();var mymonth = date.getMonth() + 1;var myweekday = date.getDate();if (mymonth < 10) {mymonth = "0" + mymonth;}if (myweekday < 10) {myweekday = "0" + myweekday;}return (myyear + "-" + mymonth + "-" + myweekday);//想要什么格式都可以隨便自己拼}var date = new Date();//date//Mon Jun 25 2018 15:32:38 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)formatDate(date);//"2018-06-25"
或者 js標(biāo)準(zhǔn)時(shí)間格式化中@joy鈺的回答:
function formatDate(dateArg) {const date = new Date(dateArg);const year = date.getFullYear();const month = date.getMonth() + 1;const day = date.getDate();const formatMonth = month < 10 ? `0${month}` : month;const formatDay = day < 10 ? `0${day}` : day;return `${year}-${formatMonth}-${formatDay}`}
2、xx 年 xx 月 xx 日 xx 時(shí) xx 分
//獲取當(dāng)前日期 時(shí)間function todayTime() {var date = new Date();var curYear = date.getFullYear();var curMonth = date.getMonth() + 1;var curDate = date.getDate();if(curMonth<10){curMonth = '0' + curMonth;}if(curDate<10){curDate = '0' + curDate;}var curHours = date.getHours();var curMinutes = date.getMinutes();var curtime = curYear + ' 年 ' + curMonth + ' 月 ' + curDate +' 日' + curHours + '時(shí) ' + curMinutes + '分 ';return curtime;}
3、英文格式日期
//獲取當(dāng)前日期 英文function todayTimeEn() {var dt = new Date();var m = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");mn = dt.getMonth();dn = dt.getDate();if(dn<10){dn = '0' + dn;}var curtime = m[mn] + " " + dn + ", " + dt.getFullYear();return curtime;}
周、月 相關(guān)的日期方法
1、一周 、本周
//近七天的日期function setDate() {var endDate = new Date();//一天的毫秒數(shù)var millisecond = 1000 * 60 * 60 * 24;var beginDate = new Date(endDate.getTime() - (7 * millisecond));$('#beginDate').val(formatDate(beginDate));$('#endDate').val(formatDate(endDate));}//本周日期 (取周一為第一天、周日為最后一天)function weekDate() {var date = new Date();var year = date.getFullYear();var month = date.getMonth();var nowDate = date.getDate();var day = date.getDay();var beginDate = new Date(year, month, nowDate - day + 1);var endDate = new Date(year, month, nowDate + (6 - day) + 1);$('#beginDate').val(formatDate(beginDate));$('#endDate').val(formatDate(endDate));}
2、 月
//本月日期function monthDate() {var date = new Date();var year = date.getFullYear();var month = date.getMonth() + 1;if (month < 10) {month = "0" + month;}var day = getDaysInOneMonth(year, month);$('#beginDate').val(year + "-" + month + "-01");$('#endDate').val(year + "-" + month + "-" + day);}//獲取某月天數(shù)function getDaysInOneMonth(year, month) {month = parseInt(month, 10);var d = new Date(year, month, 0);return d.getDate();}
獲取前天、昨天、今天、明天、后天的時(shí)間
function GetDateStr(AddDayCount) {var dd = new Date();dd.setDate(dd.getDate()+AddDayCount);//獲取AddDayCount天后的日期var y = dd.getFullYear();var m = dd.getMonth()+1;//獲取當(dāng)前月份的日期var d = dd.getDate();return y+"-"+m+"-"+d;}console.log("前天:"+GetDateStr(-2));console.log("昨天:"+GetDateStr(-1));console.log("今天:"+GetDateStr(0));console.log("明天:"+GetDateStr(1));console.log("后天:"+GetDateStr(2));console.log("大后天:"+GetDateStr(3));
獲取起始時(shí)間算起第n周的日期范圍
function weeks_enddate(startdate,n){var date = new Date(startdate);date.setTime(date.getTime() + 3600 * 1000 * 24 * 7 * n);//第n周,1、2、3、4.....return formatDate(date);}//示例:var startdate = '2018-06-27';//或者 var startdate= new Date()這樣寫也行的var week_num = 2;//n是從1開始的整數(shù)即可var enddate = weeks_enddate(startdate,week_num);console.log("enddate :"+enddate );//enddate :2018-07-11