900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > js格式化日期-年月日 时分秒

js格式化日期-年月日 时分秒

时间:2022-10-21 01:50:53

相关推荐

js格式化日期-年月日 时分秒

1.格式化年月日

// 格式化日期 年月日 var date = new Date();console.log(date.getFullYear()); // 返回当前日期的年 console.log(date.getMonth() + 1); // 月份 返回的月份小1个月 记得月份+1 呦console.log(date.getDate()); // 返回的是 几号console.log(date.getDay()); // 3 周一返回的是 1 周六返回的是 6 但是 周日返回的是 0// 我们写一个 5月 1日 星期三var year = date.getFullYear();var month = date.getMonth() + 1;var dates = date.getDate();var arr = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];var day = date.getDay();console.log('今天是:' + year + '年' + month + '月' + dates + '日 ' + arr[day]);

2.格式化时分秒

// 格式化日期 时分秒var date = new Date();console.log(date.getHours()); // 时console.log(date.getMinutes()); // 分console.log(date.getSeconds()); // 秒// 要求封装一个函数返回当前的时分秒 格式 08:08:08function getTimer() {var time = new Date();var h = time.getHours();h = h < 10 ? '0' + h : h;var m = time.getMinutes();m = m < 10 ? '0' + m : m;var s = time.getSeconds();s = s < 10 ? '0' + s : s;return h + ':' + m + ':' + s;}console.log(getTimer());

来自pink老师课堂笔记

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。