js获取日期
常用如下:
var date = new Date();//中国标准时间var year = date.getFullYear();//获取完整的年份(4位)var month = date.getMonth();//获取当前月份(0-11,0代表1月)var nowDate = date.getDate();//获取当前日(1-31)var day = date.getDay();//获取当前星期X(0-6,0代表星期天)
更多请点击JavaScript 标准库 Date或参考中第一篇。
日期的相关方法
格式化日期
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 (中国标准时间)formatDate(date);//"2018-06-25"
或者 js标准时间格式化中@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 时 xx 分
//获取当前日期 时间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 + '时 ' + curMinutes + '分 ';return curtime;}
3、英文格式日期
//获取当前日期 英文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;}
周、月 相关的日期方法
1、一周 、本周
//近七天的日期function setDate() {var endDate = new Date();//一天的毫秒数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);}//获取某月天数function getDaysInOneMonth(year, month) {month = parseInt(month, 10);var d = new Date(year, month, 0);return d.getDate();}
获取前天、昨天、今天、明天、后天的时间
function GetDateStr(AddDayCount) {var dd = new Date();dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期var y = dd.getFullYear();var m = dd.getMonth()+1;//获取当前月份的日期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));
获取起始时间算起第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开始的整数即可var enddate = weeks_enddate(startdate,week_num);console.log("enddate :"+enddate );//enddate :2018-07-11