用JavaScript的Date对象将日期格式化为年月日
最编程
2024-08-03 20:30:00
...
初始化日期年月日
var arr = new Array(); // 创建一个数组对象
var obj = new Object(); // 创建了一个对象实例
// 1. 使用Date 如果没有参数 返回当前系统的当前时间
var date = new Date();
console.log(date);//Sat Nov 28 2020 20:41:46 GMT+0800 (中国标准时间)
// 2. 参数常用的写法 数字型 2019, 10, 01 或者是 字符串型 '2019-10-1 8:8:8'
var date1 = new Date(2019, 10, 1);
console.log(date1); // 返回的是 11月 不是 10月
// Fri Nov 01 2019 00:00:00 GMT+0800 (中国标准时间)
var date2 = new Date('2019-10-1 8:8:8');
console.log(date2);
//Tue Oct 01 2019 08:08:08 GMT+0800 (中国标准时间)
JS Date对象常用的带参数初始化方式
1、用整数初始化日期对象
var date1 = new Date(2017,06,06); console.log(date1); // Thu Jul 06 2017 00:00:00 GMT+0800 (中国标准时间)
var date1 = new Date(2017,1,1); console.log(date1); // Wed Feb 01 2017 00:00:00 GMT+0800 (中国标准时间)
var date1 = new Date(2017,01-2,01); console.log(date1); // Thu Dec 01 2016 00:00:00 GMT+0800 (中国标准时间)
var date1 =new Date(2017,06,06,06,06,06); console.log(date1); // Thu Jul 06 2017 06:06:06 GMT+0800 (中国标准时间)
说明: new Date( year, month, date, hrs, min, sec) 按给定的参数创建一日期对象
2、用字符串初始化日期对象
var date2 = new Date(“2017/06/06”); console.log(date2); // Tue Jun 06 2017 00:00:00 GMT+0800 (中国标准时间)
var date2 = new Date(“2017-08-08”); console.log(date2); // Tue Aug 08 2017 08:00:00 GMT+0800 (中国标准时间)
var date2 = new Date(“2017-9-9”); console.log(date2); // Sat Sep 09 2017 00:00:00 GMT+0800 (中国标准时间)
说明:如果字符串模式不支持短横杠模式,则进行字符串替换:
var strTime=”2011-04-16”;
var date2= new Date(Date.parse(strTime.replace(/-/g, “/”))); // /-/g为正则表达式(RegExp) 对象,表示全局替换-为/。
3、用毫秒时间戳初始化日期对象
var timestamp=new Date().getTime(); console.log( new Date(timestamp) ); //Tue Jun 06 2017 11:06:59 GMT+0800 (中国标准时间)
var date3 = new Date( timestamp - 1 * 60 * 60 * 1000); console.log(date3); // Tue Jun 06 2017 10:06:59 GMT+0800 (中国标准时间)
说明:时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。时间戳唯一地标识某一刻的时间。
js中单独调用new Date(),例如document.write(new Date());
显示的结果是:Mar 31 10:10:43 UTC+0800 2012 这种格式的时间
==
测试在 游猎浏览器 获取不到时间。 不支持new Date(“2017-08-08”) 这种的短线模式。修改成斜线就好了。
格式化日期年月日
// 格式化日期 年月日
var date = new Date();
console.log(date.getFullYear()); // 返回当前日期的年 2020
console.log(date.getMonth() + 1); // 月份 返回的月份小1个月 记得月份+1 呦 11
console.log(date.getDate()); // 返回的是 几号 28
console.log(date.getDay()); // 3 周一返回的是 1 周六返回的是 6 但是 周日返回的是 0 6
// 我们写一个 2019年 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]); //今天是:2020年11月28日 星期六
格式化日期时分秒
<script>
// 格式化日期 时分秒
var date = new Date();
console.log(date.getHours()); // 时 20
console.log(date.getMinutes()); // 分 53
console.log(date.getSeconds()); // 秒 22
// 要求封装一个函数返回当前的时分秒 格式 08:08:08
function 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());//20:53:22
</script>
获得Date总的毫秒数
<script>
// 获得Date总的毫秒数(时间戳) 不是当前时间的毫秒数 而是距离1970年1月1号过了多少毫秒数
// 1. 通过 valueOf() getTime()
var date = new Date();
console.log(date.valueOf()); // 就是 我们现在时间 距离1970.1.1 总的毫秒数
console.log(date.getTime());
// 2. 简单的写法 (最常用的写法)
var date1 = +new Date(); // +new Date() 返回的就是总的毫秒数
console.log(date1);
// 3. H5 新增的 获得总的毫秒数
console.log(Date.now());
</script>